gpt4 book ai didi

c - 是什么导致了 make 中 undefined reference ?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:23 24 4
gpt4 key购买 nike

以下 make 输出显示 undefined reference ,我不确定是什么原因造成的。有人可以帮忙吗?

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe
make[2]: Entering directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/main.o.d
gcc -std=c99 -c -g -I../mongodb-mongo-c-driver/src/\*.c -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/main.o.d -o build/Debug/Cygwin_4.x-Windows/main.o main.c
mkdir -p dist/Debug/Cygwin_4.x-Windows
gcc -std=c99 -o dist/Debug/Cygwin_4.x-Windows/mongodrivertest build/Debug/Cygwin_4.x-Windows/main.o
nbproject/Makefile-Debug.mk:61: recipe for target `dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe' failed
make[2]: Leaving directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
nbproject/Makefile-Debug.mk:58: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/g/workspace/c_cpp/MongoDriverTest'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
build/Debug/Cygwin_4.x-Windows/main.o: In function `main':
/cygdrive/g/workspace/c_cpp/MongoDriverTest/main.c:19: undefined reference to `_mongo_connect'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/mongodrivertest.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

这是我的 main.c 的内容:

#include <stdio.h>
#include <stdlib.h>
#include "../mongodb-mongo-c-driver/src/mongo.h"

int main(int argc, char** argv) {
int status;
mongo conn[1];

status=mongo_connect(conn, "127.0.0.1", 27017);
return EXIT_SUCCESS;
}

两天前还可以,我重装了操作系统,现在已经不行了,我好像没找到原因。 mongo.h 存在,mongo.o 也存在。 mongo_connect 在 mongo.c 中。有什么想法吗?

最佳答案

您的链接行是:

gcc -std=c99 -o dist/Debug/Cygwin_4.x-Windows/mongodrivertest build/Debug/Cygwin_4.x-Windows/main.o  

它不会告诉 GCC 从哪里收集 mongo_connect()。需要在命令行指定Mongo库。

给定源代码中的 include 行:

#include "../mongodb-mongo-c-driver/src/mongo.h"

您可以添加选项:

-L../mongodb-mongo-c-driver/lib -lmongo

到链接行。位置和图书馆名称都是猜测。这将从指定目录中获取 libmongo.dlllibmongo.lib

如果在 ../mongodb-mongo-c-driver 目录下找不到该库,则可能需要构建并安装它。或者,它可能已经安装,您只需要确保您引用的是正确的安装位置。


此外,作为一般规则,请避免在源代码中使用类似的路径名。您应该指定:

#include "mongo.h"

并提供一个编译行选项来指定在哪里寻找它:

-I../mongodb-mongo-c-driver/src

另请参阅:What are the benefits of a relative path such as #include "../include/header.h" for a header? .

关于c - 是什么导致了 make 中 undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10676520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com