什么导致未定义的参考文件?

时间:2012-05-20 19:31:48

标签: c

以下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中。有什么想法吗?

1 个答案:

答案 0 :(得分: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行:

#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?