gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:40:36 27 4
gpt4 key购买 nike

我正在尝试为构建目标文件的多文件 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 时,我收到一连串的链接器错误(多个定义、 undefined reference )。其他 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编译。

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

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

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

关于尝试从使用 makefile 生成的目标文件构建时出现 C++ 链接器错误。当我刚刚构建它时不会发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com