编译 C++ 时的 Makefile 问题程序

发布于 2024-09-03 19:33:03 字数 2258 浏览 4 评论 0原文

我最近在 Cygwin 上编译并运行了 MySQL,并从网上得到了一个简单的测试示例来验证它是否有效。测试示例编译并运行成功。

然而,当将 MySQL 合并到我的业余爱好项目中时,它无法编译,我相信这是由于 Makefile 的设置方式所致,我没有使用 Makefile 的经验,在阅读有关它们的教程后,我有更好的掌握,但仍然可以'不能让它正常工作。

当我尝试编译我的爱好项目时,我收到错误,例如:

Obj/Database.o:Database.cpp:(.text+0x492): undefined reference to `_mysql_insert_id'
Obj/Database.o:Database.cpp:(.text+0x4c1): undefined reference to `_mysql_affected_rows'
collect2: ld returned 1 exit status
make[1]: *** [build] Error 1
make: *** [all] Error 2

这是我的 Makefile,它在我尝试将 MySQL 支持放入项目之前用于编译和构建源代码。 LIBMYSQL 路径是正确的,由“mysql_config”验证。

COMPILER = g++
WARNING1 = -Wall -Werror -Wformat-security -Winline -Wshadow -Wpointer-arith
WARNING2 = -Wcast-align -Wcast-qual -Wredundant-decls 
LIBMYSQL = -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient
DEBUGGER = -g3
OPTIMISE = -O

C_FLAGS =  $(OPTIMISE) $(DEBUGGER) $(WARNING1) $(WARNING2) -export-dynamic $(LIBMYSQL)
L_FLAGS = -lz -lm -lpthread -lcrypt $(LIBMYSQL)

OBJ_DIR = Obj/
SRC_DIR = Source/
MUD_EXE = project
MUD_DIR = TestP/
LOG_DIR = $(MUD_DIR)Files/Logs/

ECHOCMD = echo -e
L_GREEN = \e[1;32m
L_WHITE = \e[1;37m
L_BLUE  = \e[1;34m
L_RED   = \e[1;31m
L_NRM   = \e[0;00m

DATE    = `date +%d-%m-%Y`
FILES   = $(wildcard $(SRC_DIR)*.cpp)
C_FILES = $(sort $(FILES))
O_FILES = $(patsubst $(SRC_DIR)%.cpp, $(OBJ_DIR)%.o, $(C_FILES))

all:
     @$(ECHOCMD) "  Compiling $(L_RED)$(MUD_EXE)$(L_NRM).";
     @$(MAKE) -s build

build: $(O_FILES)
     @rm -f $(MUD_EXE)
     $(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)
     @echo "  Finished Compiling $(MUD_EXE).";
     @chmod g+w $(MUD_EXE)
     @chmod a+x $(MUD_EXE)
     @chmod g+w $(O_FILES)

$(OBJ_DIR)%.o: $(SRC_DIR)%.cpp
     @echo "  Compiling $@";
     $(COMPILER) -c $(C_FLAGS) $< -o $@

.cpp.o:
     $(COMPILER) -c $(C_FLAGS) $<

clean:
     @echo "  Complete compile on $(MUD_EXE).";
     @rm -f $(OBJ_DIR)*.o $(MUD_EXE)
     @$(MAKE) -s build

我喜欢Makefile的功能,而不是吐出所有参数等,它只是吐出“编译[文件名]”等。

如果我将-c添加到L_FLAGS,那么它会编译(我认为),但会吐出东西就像:

g++: Obj/Database.o: linker input file unused because linking not done

在谷歌上一整天的尝试和研究之后,我还没有更接近解决我的问题,所以我来找你们,看看你们是否可以向我解释为什么会发生这一切,如果可能的话,解决的步骤。

问候, 史蒂夫

I recently got MySQL compiled and working on Cygwin, and got a simple test example from online to verify that it worked. The test example compiled and ran successfully.

However, when incorporating MySQL in a hobby project of mine it isn't compiling which I believe is due to how the Makefile is setup, I have no experience with Makefiles and after reading tutorials about them, I have a better grasp but still can't get it working correctly.

When I try and compile my hobby project I recieve errors such as:

Obj/Database.o:Database.cpp:(.text+0x492): undefined reference to `_mysql_insert_id'
Obj/Database.o:Database.cpp:(.text+0x4c1): undefined reference to `_mysql_affected_rows'
collect2: ld returned 1 exit status
make[1]: *** [build] Error 1
make: *** [all] Error 2

Here is my Makefile, it worked with compiling and building the source before I attempted to put in MySQL support into the project. The LIBMYSQL paths are correct, verified by 'mysql_config'.

COMPILER = g++
WARNING1 = -Wall -Werror -Wformat-security -Winline -Wshadow -Wpointer-arith
WARNING2 = -Wcast-align -Wcast-qual -Wredundant-decls 
LIBMYSQL = -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient
DEBUGGER = -g3
OPTIMISE = -O

C_FLAGS =  $(OPTIMISE) $(DEBUGGER) $(WARNING1) $(WARNING2) -export-dynamic $(LIBMYSQL)
L_FLAGS = -lz -lm -lpthread -lcrypt $(LIBMYSQL)

OBJ_DIR = Obj/
SRC_DIR = Source/
MUD_EXE = project
MUD_DIR = TestP/
LOG_DIR = $(MUD_DIR)Files/Logs/

ECHOCMD = echo -e
L_GREEN = \e[1;32m
L_WHITE = \e[1;37m
L_BLUE  = \e[1;34m
L_RED   = \e[1;31m
L_NRM   = \e[0;00m

DATE    = `date +%d-%m-%Y`
FILES   = $(wildcard $(SRC_DIR)*.cpp)
C_FILES = $(sort $(FILES))
O_FILES = $(patsubst $(SRC_DIR)%.cpp, $(OBJ_DIR)%.o, $(C_FILES))

all:
     @$(ECHOCMD) "  Compiling $(L_RED)$(MUD_EXE)$(L_NRM).";
     @$(MAKE) -s build

build: $(O_FILES)
     @rm -f $(MUD_EXE)
     $(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)
     @echo "  Finished Compiling $(MUD_EXE).";
     @chmod g+w $(MUD_EXE)
     @chmod a+x $(MUD_EXE)
     @chmod g+w $(O_FILES)

$(OBJ_DIR)%.o: $(SRC_DIR)%.cpp
     @echo "  Compiling $@";
     $(COMPILER) -c $(C_FLAGS) 
lt; -o $@

.cpp.o:
     $(COMPILER) -c $(C_FLAGS) 
lt;

clean:
     @echo "  Complete compile on $(MUD_EXE).";
     @rm -f $(OBJ_DIR)*.o $(MUD_EXE)
     @$(MAKE) -s build

I like the functionality of the Makefile, instead of spitting out all the arguments etc, it just spits out the "Compiling [Filename]" etc.

If I add -c to the L_FLAGS then it compiles (I think) but instead spits out stuff like:

g++: Obj/Database.o: linker input file unused because linking not done

After a full day of trying and research on google, I'm no closer to solving my problem, so I come to you guys to see if you can explain to me why all this is happening and if possible, steps to solve.

Regards,
Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2024-09-10 19:33:03

尝试更改

$(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)

$(COMPILER) -o $(MUD_EXE) $(O_FILES) $(L_FLAGS)

链接器按照指定的顺序搜索和处理库和目标文件。因此,当您在目标文件之前提到库时,目标文件中使用的函数可能不会从库中加载。

Try changing

$(COMPILER) -o $(MUD_EXE) $(L_FLAGS) $(O_FILES)

to

$(COMPILER) -o $(MUD_EXE) $(O_FILES) $(L_FLAGS)

The linker searches and processes libraries and object files in the order they are specified. Thus when you mention the libraries before the object files, the functions used in the object files may not be loaded from the libraries.

执笔绘流年 2024-09-10 19:33:03

您是否尝试过分别使用内置的 mysql_config --cflags 和 mysql_config --libs ?

Have you tried using the built-in mysql_config --cflags and mysql_config --libs, respectively?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文