简体   繁体   English

编译Mongo c驱动程序示例程序

[英]Compiling Mongo c driver example program

I am beginning to learn about using the mongo-c-driver, but I have been having compilation issues when I run 我开始学习使用mongo-c-driver,但是当我运行时我遇到了编译问题

gcc -g -Wall -Werror -Isrc --std=c99 mongo-c-driver/src/*.c -I mongo-c-driver/src/ intro.c -lmongoc 

I have already looked over the other stackoverflow post, found here cannot compile mongo-c-driver example , it did not help. 我已经查看了其他stackoverflow帖子,发现这里无法编译mongo-c-driver的例子 ,它没有帮助。 Here is the output from my compiler 这是我的编译器的输出

mongo-c-driver/src/bcon.c:37:12: error: ‘bcon_error’ defined but not used [-Werror=unused-function]
mongo-c-driver/src/bcon.c:378:13: error: ‘bcon_json_print’ defined but not used [-Werror=unused-function]
cc1: all warnings being treated as errors
mongo-c-driver/src/env.c: In function ‘mongo_env_socket_connect’:
mongo-c-driver/src/env.c:319:21: error: storage size of ‘ai_hints’ isn’t known
mongo-c-driver/src/env.c:340:5: error: implicit declaration of function ‘getaddrinfo’ [-Werror=implicit-function-declaration]
mongo-c-driver/src/env.c:342:9: error: implicit declaration of function ‘gai_strerror’ [-Werror=implicit-function-declaration]
mongo-c-driver/src/env.c:347:60: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:348:36: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:348:55: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:348:76: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:354:45: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:354:62: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:368:20: error: dereferencing pointer to incomplete type
mongo-c-driver/src/env.c:381:5: error: implicit declaration of function ‘freeaddrinfo’ [-Werror=implicit-function-declaration]
mongo-c-driver/src/env.c:319:21: error: unused variable ‘ai_hints’ [-Werror=unused-variable]
cc1: all warnings being treated as errors
intro.c: In function ‘main’:
intro.c:9:7: error: enumeration value ‘MONGO_CONN_SUCCESS’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_CONN_ADDR_FAIL’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_CONN_BAD_SET_NAME’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_CONN_NO_PRIMARY’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_IO_ERROR’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_SOCKET_ERROR’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_READ_SIZE_ERROR’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_COMMAND_FAILED’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_WRITE_ERROR’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_NS_INVALID’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_BSON_INVALID’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_BSON_NOT_FINISHED’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_BSON_TOO_LARGE’ not handled in switch [-Werror=switch]
intro.c:9:7: error: enumeration value ‘MONGO_WRITE_CONCERN_INVALID’ not handled in switch [-Werror=switch]
cc1: all warnings being treated as errors

Update 更新

Also, if I decide to run 另外,如果我决定跑步

gcc --std=c99 -Wall intro.c -lmongoc

my program will compile, but will give me the error 我的程序将编译,但会给我错误

$ ./a.out 
./a.out: error while loading shared libraries: libmongoc.so.0.7: cannot open shared object file: No such file or directory

Another way to fix this issue, at least in fedora 18, is to run the following commands 解决此问题的另一种方法,至少在fedora 18中,是运行以下命令

su -c "echo /usr/local/lib > /etc/ld.so.conf.d/mongoc.conf"
su -c "ldconfig"

which correctly link the libraries from mongo-c-driver. 从mongo-c-driver正确链接库。

I have no idea why this example will not compile, but if I copy the src directory and make a lib directory, where all of the compiled binaries for the driver lie, the I can compile the program using 我不知道为什么这个例子不会编译,但如果我复制src目录并创建一个lib目录,其中驱动程序的所有已编译二进制文件都在,我可以使用

gcc --std=c99 -o test_program -Llib/ -Isrc/ test.c lib/libmongoc.a

where the directories are setup as follows 目录设置如下

--test.c
--lib
|---libbson.a
|---libbson.so
|---libmongoc.a
|---libmongoc.so
--src
|---bcon.h
|---bon.c
|---env.c
|---env.h
|---etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM