尝试从使用 makefile 生成的目标文件构建时出现 C++ 链接器错误。当我刚刚构建它时不会发生

C++ linker error when trying to build from object files made using makefile. Does not occur when I just build it

我正在尝试为构建目标文件的多文件 C++ 项目制作联编文件,然后从这些目标文件构建我的应用程序。

当我使用命令时它没有任何错误:

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)

我已经尝试过类似主题的可能解决方案。没有骰子。任何帮助都会很棒。如果需要,我可以 post 更多内容。

这是链接器错误:

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 编译。

您应该完全放弃该规则中的配方,并使用其内置的 %.o: %.cpp 规则。 (你需要更新你的 VPATH 东西来告诉 make 如何找到你的源文件来做到这一点。)

或者,您需要在那里使用实际的模式规则 $(OUT)/%.o: $(SRC)/%.cpp,等等

makefile 中还有其他低效之处(尤其是您多次 make 运行 pkg-config,每次都需要扩展 $(CXXFLAGS)$(LIBS)).但这些有点超出范围。