当尝试从使用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 文件 目标      更新时间:2023-10-16

我正试图为构建目标文件的多文件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))。但是这些都超出了范围。

相关文章: