0

下面的 make 输出显示了一个未定义的引用,我不确定是什么原因造成的。有人可以帮忙吗?

"/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 中。任何想法?

4

1 回答 1

4

您的链接线是:

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

它没有告诉 GCC 从哪里收集mongo_connect()。您需要在命令行中指定 Mongo 库。

鉴于源代码中的包含行:

#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

另请参阅:相对路径(例如#include "../include/header.h"标题)有什么好处?.

于 2012-05-20T19:35:45.577 回答