使用与makefile链接的共享库
Use shared library linked with a makefile
我正在尝试用以下make文件编译我的程序。我在网上找到了一个模板,我正在尝试使用它。我只修改了# Main entry point
制作文件
# Define executable name
BIN = CLI_DeskManager
# Define source files
SRCS = main.cpp shell.cpp
# Define header file paths
INCPATH = -I./ -I/usr/include/X11/exensions -I/usr/include/X11 -I/home/julien/Documents/DeskManagerDll
# Define the -L library path(s)
LDFLAGS = -L/usr/X11R6/lib -L/home/julien/Documents/DeskManagerDll
# Define the -l library name(s)
LIBS = -lX11 -lXext -Wl,--no-as-needed -lDeskManager -lpthread
# Only in special cases should anything be edited below this line
OBJS = $(CPP_SRCS:.cpp=.o)
CXXFLAGS = -Wall -ansi -pedantic -std=c++11 -pthread
DEP_FILE = .depend
# Main entry point
#
all: depend $(BIN)
# For linking object file(s) to produce the executable
#
$(BIN): $(OBJS)
@echo Linking $@
@$(CXX) $^ $(LDFLAGS) $(LIBS) -o $@
# For compiling source file(s)
#
.cpp.o:
@echo Compiling $<
@$(CXX) -c $(CXXFLAGS) $(INCPATH) $<
# For cleaning up the project
#
clean:
$(RM) $(OBJS)
distclean: clean
$(RM) $(BIN)
$(RM) $(DEP_FILE)
# For determining source file dependencies
#
depend: $(DEP_FILE)
@touch $(DEP_FILE)
$(DEP_FILE):
@echo Generating dependencies in $@
@-$(CXX) -E -MM $(CXXFLAGS) $(INCPATH) $(SRCS) >> $(DEP_FILE)
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
ifeq (,$(findstring distclean,$(MAKECMDGOALS)))
-include $(DEP_FILE)
endif
endif
错误
但是我有很多错误,比如下面这个
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
但是在这个线程中soru说重要的是:
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [CLI_DeskManager] Error 1
附加信息
我已经成功地用Code::块编译了它,这是构建日志。
g++ -Wall -fexceptions -std=c++11 -g -pthread -I/usr/include/X11/extensions -I/usr/include/X11 -I../DeskManagerDll -c /home/julien/Documents/CommandLineInterface_DeskManager/main.cpp -o obj/Debug/main.o
g++ -Wall -fexceptions -std=c++11 -g -pthread -I/usr/include/X11/extensions -I/usr/include/X11 -I../DeskManagerDll -c /home/julien/Documents/CommandLineInterface_DeskManager/shell.cpp -o obj/Debug/shell.o
g++ -L-L/usr/lib/i386-linux-gnu -o bin/Debug/CommandLineInterface_DeskManager obj/Debug/main.o obj/Debug/shell.o -L/usr/X11R6/lib -lX11 -lXext -Wl,--no-as-needed -lpthread /home/julien/Documents/DeskManagerDll/bin/Debug/libDeskManagerDll.so
我在StackOverflow上找到了答案,但是我不理解它们。
链接,链接,链接,链接
我做错了什么?
谢谢。
在Lijat回答后更新
建议修改后的Makefile
# Define executable name
BIN = CLI_DeskManager
# Define source files
SRCS = main.cpp shell.cpp
# Define header file paths
INCPATH = -I./ -I/usr/include/X11/exensions -I/usr/include/X11 -I/home/julien/Documents/DeskManagerDll
# Define the -L library path(s)
LDFLAGS = -L/usr/X11R6/lib -L/home/julien/Documents/DeskManagerDll/bin/Debug
# Define the -l library name(s)
LIBS = -lX11 -lXext -Wl,--no-as-needed -lDeskManagerDll -lpthread
# Only in special cases should anything be edited below this line
OBJS = $(CPP_SRCS:.cpp=.o)
CXXFLAGS = -Wall -ansi -pedantic -std=c++11 -pthread
DEP_FILE = .depend
# Main entry point
#
all: depend $(BIN)
# For linking object file(s) to produce the executable
#
$(BIN): $(OBJS)
echo Linking $@
$(CXX) $^ $(LDFLAGS) $(LIBS) -o $@
# For compiling source file(s)
#
.cpp.o:
echo Compiling $<
$(CXX) -c $(CXXFLAGS) $(INCPATH) $< -o $@
# For cleaning up the project
#
clean:
$(RM) $(OBJS)
distclean: clean
$(RM) $(BIN)
$(RM) $(DEP_FILE)
# For determining source file dependencies
#
depend: $(DEP_FILE)
touch $(DEP_FILE)
$(DEP_FILE):
echo Generating dependencies in $@
-$(CXX) -E -MM $(CXXFLAGS) $(INCPATH) $(SRCS) >> $(DEP_FILE)
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
ifeq (,$(findstring distclean,$(MAKECMDGOALS)))
-include $(DEP_FILE)
endif
endif
全制作输出。我很抱歉它是用法语写的。在我的问题的第一部分中,只有两个不同的句子被翻译了。
julien@julien-VirtualBox:~/Documents/CommandLineInterface_DeskManager$ make
touch .depend
echo Linking CLI_DeskManager
Linking CLI_DeskManager
g++ -L/usr/X11R6/lib -L/home/julien/Documents/DeskManagerDll/bin/Debug -lX11 -lXext -Wl,--no-as-needed -lDeskManagerDll -lpthread -o CLI_DeskManager
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 0 a un index de symbole 11 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 1 a un index de symbole 12 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 2 a un index de symbole 2 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 3 a un index de symbole 2 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 4 a un index de symbole 11 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 5 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 6 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 7 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 8 a un index de symbole 12 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 9 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 10 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 11 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 12 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 13 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 14 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 15 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 16 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 17 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 18 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 19 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 20 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 21 a un index de symbole 22 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): réadressage 0 a un index de symbole 2 invalide
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: dans la fonction « _start »:
(.text+0x18): référence indéfinie vers « main »
collect2: error: ld returned 1 exit status
make: *** [CLI_DeskManager] Erreur 1
有点不清楚您想要实现什么,您是想创建一个共享库还是只是想使用一个共享库?
Code::blocks日志提示第二个选项,如果是这种情况,我注意到你有libDeskManagerDll。所以在Code::blocks log和makefile中的-lDeskManager中,为了使它们相同,我希望是-lDeskManagerDll
我也觉得这行很奇怪
@$(CXX) -c $(CXXFLAGS) $(INCPATH) $<
我希望它看起来像
@$(CXX) -c $(CXXFLAGS) $(INCPATH) $< -o $@
如果这不能解决问题,你可以在makefile中删除行开头的@符号并发布make的完整输出吗?
从您的更新中可以清楚地看出,make不包括链接命令中的目标文件。仔细检查
一行SRCS = main.cpp shell.cpp
定义了要与
一起使用的源文件。OBJS = $(CPP_SRCS:.cpp=.o)
将该行改为
OBJS = $(SRCS:.cpp=.o)
应该让你更接近你想要的
相关文章:
- 无法在Ubuntu上将共享库与Eclipse链接
- CMake - 更改共享库链接选项
- 与共享库链接时未定义的引用
- 如何在 Linux 中构建共享库时使未定义引用的链接器失败
- 共享对象、符号、C/C++ 库链接和加载
- 如果同时存在共享库和动态库,则链接器将首选哪个库?
- 如何防止 CMake 在构建时(而不是在安装时)为共享库创建符号链接?
- c++ 链接器错误"针对'.rodata.str1.1'的重新定位R_X86_64_32"链接 CXX 共享库 libsrt.so
- 停止共享库链接丢弃未使用的类
- 链接二进制库或共享库时,为什么必须传入我依赖的共享库?
- 使用共享库编译可执行文件时仅链接所需的符号
- C++ 为什么要级联共享对象链接
- 与 32 位共享对象的链接问题
- 使用 pybind11 构建库,链接其他共享库
- 运行程序时找不到共享对象库,但在编译过程中链接了它
- 链接共享库依赖项未在 ldd 中列出
- 将共享对象与静态库链接
- CMake:修改共享库时的冗余链接
- 与MinGW的静态和动态/共享链接
- Qt-多个QtableWidget可以共享链接的项目