简体   繁体   English

mongoc_init未定义参考

[英]mongoc_init undefined reference

I am trying to use the mongo C driver in a Trivia server program I am making to use it to track login information scores etc. 我试图在Trivia服务器程序中使用mongo C驱动程序,以便使用它来跟踪登录信息得分等。

It took me a decent amount of time to figure out how to compile the stuff in a makefile but I was able to include mongoc.h so things like mongoc_client work fine. 我花了相当多的时间来弄清楚如何在makefile中编译这些东西,但是我能够包含mongoc.h,所以mongoc_client之类的东西可以正常工作。 As soon as I try to actually use a function though, such as mongoc_init I get 一旦我尝试实际使用一个函数,例如mongoc_init,我就会得到

> undefined reference to `mongoc_init'  

I feel like things should be linked though since the mongoc.h include all other header files and it looks like it is linking properly. 我觉得应该链接东西,因为mongoc.h包含所有其他头文件,并且看起来链接正确。 Here is my makefile - I'm not sure what to do or even what information to provide to solve this issue. 这是我的makefile-我不确定该怎么做,甚至不确定要提供什么信息来解决此问题。

client:
    make -f makefile.client

server:
    make -f makefile.server

clean:
    rm -f *~ *.o tserve core tclient core *.tar *.zip *.gzip *.bzip *.gz  

^ Makefile ^ Makefile

C = gcc
CFLAGS = -g -Wall
LDFLAGS = -lpthread
INFO = -L/usr/local/lib -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -lmongoc-1.0 -lbson-1.0

all: tserve

csapp.o: csapp.c csapp.h
        $(CC) $(CFLAGS) -c csapp.c

tserve.o: tserve.c readq.h csapp.h
        $(CC) $(CFLAGS) -c tserve.c $(INFO)

readq.o: readq.c readq.h csapp.h
        $(CC) $(CFLAGS) -c readq.c

tserve: tserve.o readq.o csapp.o

^ makefile.server ^ makefile.server

C = gcc
CFLAGS = -g -Wall
LDFLAGS = -lpthread

all: tclient

csapp.o: csapp.c csapp.h
        $(CC) $(CFLAGS) -c csapp.c

tclient.o: tclient.c csapp.h
        $(CC) $(CFLAGS) -c tclient.c

tclient: tclient.o csapp.o

^ makefile.client ^ makefile.client

If it is worth noting - the system is question had the driver installed using the following code 如果值得注意的话-系统使用以下代码安装驱动程序是有问题的

system("wget https://github.com/mongodb/mongo-c-driver/releases/download/1.0.2/mongo-c-driver-1.0.2.tar.gz");
system("tar -xzf mongo-c-driver-1.0.2.tar.gz");
system("rm mongo-c-driver-1.0.2.tar.gz");
if (chdir("mongo-c-driver-1.0.2"))
    perror("error:");
system("./configure --prefix=/usr --libdir=/usr/lib64");
system("make");
system("sudo make install");

the code is also at https://www.github.com/decix/TriviaServer 该代码也位于https://www.github.com/decix/TriviaServer

You only use the INFO variable, which contains -lmongoc-1.0 -lbson-1.0 (which is what you need), to build tserve.o . 您仅使用INFO变量(包含-lmongoc-1.0 -lbson-1.0 (您需要的))来构建tserve.o In this build step, the linker is not involved, so the -l options are not evaluated and don't do anything. 在此构建步骤中,不涉及链接器,因此-l选项不会被评估并且不会执行任何操作。 Instead, you need to specify them when you build tserve . 相反,在构建tserve时需要指定它们。

You could solve this by supplying a recipe for tserve that does this, such as 你可以通过提供的配方解决这个tserve ,这是否如

tserve: tserve.o readq.o csapp.o
    $(CC) $(LDFLAGS) $(INFO) -o $@ $+

...but it would be better, in my opinion, to split INFO and put the parts into the variables that the predefined rules use for the relevant purposes. ...但是我认为,最好拆分INFO并将这些部分放入预定义规则用于相关目的的变量中。 Then your whole Makefile could be replaced with 然后您的整个Makefile可以替换为

# C compiler
CC       = gcc
# C compiler flags
CFLAGS   = -g -Wall
# C preprocessor flags
CPPFLAGS = -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -pthread
# Linker flags
LDFLAGS  = -L/usr/local/lib -pthread
# Libraries
LDLIBS   = -lmongoc-1.0 -lbson-1.0

all: tserve

csapp.o: csapp.c csapp.h
tserve.o: tserve.c readq.h csapp.h
readq.o: readq.c readq.h csapp.h

tserve: tserve.o readq.o csapp.o

The .o files will then be built through implicit rules that generate them from corresponding .c files and use CPPFLAGS and CFLAGS , saving you the trouble of specifying the recipes explicitly -- the rules only exist to track dependencies -- and the linking step uses an implicit rule that uses LDFLAGS and LDLIBS . 然后,将通过隐式规则构建.o文件, .o式规则从相应的.c文件生成它们并使用CPPFLAGSCFLAGS ,从而省去了显式指定配方的麻烦-这些规则仅用于跟踪依赖关系-链接步骤使用使用LDFLAGSLDLIBS的隐含规则。 Note that I supplied -pthread in both CPPFLAGS and LDFLAGS because it is relevant for both the preprocessor and the linker. 请注意,我在CPPFLAGSLDFLAGS中都提供了-pthread ,因为它与预处理器和链接器都相关。

For further information, see the bit about implicit rules in the GNU make manual, and speaking of dependency tracking, you may find this article about automating it enlightening. 有关更多信息,请参见GNU make手册中有关隐式规则的部分,以及有关依赖项跟踪的知识,您可能会发现本文对如何使它自动化具有启发性。

the following file is expected to be named Makefile.mak


# note: with very minor changes this should work for most C projects
#       irregardless of the OS being used

SHELL := /bin/sh

.PHONY: all clean

#
# macro for all *.c files
# (NOTE:
# (the following 'wildcard' will pick up ALL .c files
# (like FileHeader.c and FunctionHeader.c
# (which should not be part of the build
# (so be sure no unwanted .c files in directory
# (or change the extension
#
SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
INC := $(SRC:.c=.h)


CC       := /usr/bin/gcc
MAKE     := /usr/bin/make


name := tserve

CFLAGS := -c -g -std=c11 -Wall -Wextra -pedantic-errors
#CPPFLAGS += =MD

# when #(DEBUG) is used in the compile/link steps,
# them max info for the gdb debugger is generated
DEBUG   := -ggdb3
LDFLAGS := -L/lib -L/usr/lib -L/usr/local/lib

LIBS :=  -lpthread -lmongoc-1.0 -lbson-1.0

INC := -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0
name := tserve

all: $(name)


#
# link the .o files into the executable
# using the linker flags
# -- explicit rule
#
$(name): $(OBJ)
    #
    # ======= $(name) Link Start =========
    $(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
    # ======= $(name) Link Done ==========
    #

#
#create dependancy files
# -- inference rule
#
%.d: %.c Makefile.mak
    #
    # ========= START $< TO $@ =========
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                      \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
    rm -f $@.$$$$
    # ========= END $< TO $@ =========

#
# compile the .c file into .o files using the compiler flags
# -- inference rule
#
%.o: %.c %.d
    #
    # ========= START $< TO $@ =========
    $(CC) $(CCFLAGS) -c $< -o $@ -I. $(INC)
    # ========= END $< TO $@ =========
    #


clean:
    # ========== CLEANING UP ==========
    rm -f *.o
    rm -f $(name).map
    rm -f $(name)
    rm -f *.d
    # ========== DONE ==========


ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif

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

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