当尝试从使用makefile生成的目标文件构建时,c++链接器错误.在我刚建立的时候不会发生
C++ linker error when trying to build from object files made using makefile. Does not occur when I just build it
我正试图为构建目标文件的多文件c++项目制作一个makefile,然后从这些目标文件构建我的应用程序。
当我使用命令
时,它没有任何错误:all: src/main.cpp src/main_funcs.cpp src/menu.cpp src/logging.cpp
src/runonce.cpp src/wordify.cpp src/vte.cpp
g++ -Wall `pkg-config --cflags gtk+-3.0, vte-2.91, glib-2.0, gio-2.0`
-std=c++14 -o "updater" src/main.cpp src/main_funcs.cpp
src/logging.cpp src/menu.cpp src/runonce.cpp
src/wordify.cpp src/vte.cpp `pkg-config
--libs gtk+-3.0, vte-2.91, glib-2.0, gio-2.0` -lboost_date_time
-lboost_system -lpthread
但是当我尝试使用我的其他makefile时,我得到了一连串的链接器错误(多个定义,未定义的引用)。其他makefile的相关部分如下:
IDIR = include
ODIR = obj
SRC_DIR = src
CXX = g++
CXXFLAGS = -Wall -std=c++14
`pkg-config --cflags gtk+-3.0, vte-2.91, glib-2.0, gio-2.0`
LIBS = `pkg-config --libs gtk+-3.0, vte-2.91, glib-2.0, gio-2.0`
-lboost_date_time -lboost_system -lpthread
_FILES = main.cpp main_funcs.cpp menu.cpp logging.cpp wordify.cpp
runonce.cpp vte.cpp
FILES = $(patsubst %,$(SRC_DIR)/%,$(_FILES))
_DEPS = include.h logging.h main_funcs.h main_gtk.h menu.h runonce.h
vte_include.h wordify.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = main.o main_funcs.o menu.o logging.o wordify.o runonce.o vte.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
VPATH = $(ODIR):$(IDIR)
OUT = -o $(BDIR)/update
$(ODIR)/%.o: $(FILES) $(DEPS)
$(CXX) $(CXXFLAGS) -c -o $@ $< $(LIBS)
all: $(OBJ)
$(CXX) $(CXXFLAGS) $(OUT) $^ $(LIBS)
我已经从类似的线程尝试了可能的解决方案。不行。任何帮助都太好了。如果我需要的话,我可以发更多的东西。
链接器错误:
obj/main_funcs.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/main_funcs.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/main_funcs.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/main_funcs.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/menu.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/menu.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/menu.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/menu.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/logging.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/logging.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/logging.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/logging.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/wordify.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/wordify.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/wordify.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/wordify.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/runonce.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/runonce.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/runonce.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/runonce.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/vte.o:(.bss+0x0): multiple definition of `rank_run_count'
obj/main.o:(.bss+0x0): first defined here
obj/vte.o:(.bss+0x4): multiple definition of `update_run_count'
obj/main.o:(.bss+0x4): first defined here
obj/vte.o: In function `notroot(_GtkWidget*)':
main.cpp:(.text+0x0): multiple definition of `notroot(_GtkWidget*)'
obj/main.o:main.cpp:(.text+0x0): first defined here
obj/vte.o: In function `main':
main.cpp:(.text+0x4f1): multiple definition of `main'
obj/main.o:main.cpp:(.text+0x4f1): first defined here
obj/main.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/main_funcs.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/menu.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/logging.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/wordify.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/runonce.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
obj/vte.o: In function `mainwin(_GtkApplication*, void*)':
main.cpp:(.text+0x219): undefined reference to `main_menu_render()'
main.cpp:(.text+0x29d): undefined reference to `rank_callback(_GtkWidget*, void*, _GtkWidget*)'
main.cpp:(.text+0x2d4): undefined reference to `update_callback(_GtkWidget*, void*, _GtkWidget*)'
collect2: error: ld returned 1 exit status
Makefile:41: recipe for target 'all' failed
make: *** [all] Error 1
你的$(ODIR)/%.o: $(FILES) $(DEPS)
规则是大错特错
你告诉make每个输出的.o
文件都依赖于每个 .cpp
和.h
文件。
此外,该目标的规则只使用$<
,这是$(FILES)
中的第一个先决条件。
所以你输出的每一个.o
文件都是简单的main.cpp
编译。
您应该完全删除该规则中的配方,并让make使用其内置的%.o: %.cpp
规则。(你需要更新你的VPATH
的东西告诉make如何找到你的源文件来做到这一点。)
或者,您需要在$(OUT)/%.o: $(SRC)/%.cpp
等处使用实际的模式规则。
在makefile中还有其他效率低下的地方(其中最重要的是你必须多次运行pkg-config
,每次它都需要扩展$(CXXFLAGS)
和$(LIBS)
)。但是这些都超出了范围。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接