本文介绍了建立在GCC上的函数多重定义链接器错误 - Makefile问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我试图用一个Makefile来建立一个项目,而且一般来说对于makefile来说,它是相对较新的。链接负载函数时出现多个定义错误,而且我很确定这是由于我的makefile造成的。我不能发布很多项目,因为它非常大,但makefile文件位于下面,是否有任何突出的显示错误? 我在头文件中定义了一些声明+定义的函数,并将它们的定义移动到cpp中,从链接器错误中移除了这些函数 - 但我无法为所有这些函数执行此操作(编辑:被多重定义的函数的其余部分不在标题中,它们作为标准在cpp / cc文件中,说我不能为所有这些文件做这件事暗示它们都是这样的,对不起),作为很大一部分是我无法编辑的代码库。在代码中不应该有任何错误,因为它在单独的项目中没有我的添加(没有一个导致链接器错误),所以我认为它必须是我的生成文件,但我无法弄清楚我做错了什么。任何想法? #编译器 CXX = g ++ #链接器设置 LDFLAGS = -lGL -lGLU -lXext -lX11 $ b $可执行文件名称 EXEC = SplotchPreviewer #编译优化 OPTIMIZE = -std = c ++ 98 -pedantic -Wno-long-long -Wfatal-errors -Wextra -Wall -Wstrict-aliasing = 2 -Wundef -Wshadow -Wwrite-strings -Wredundant-decls -Woverloaded-virtual -Wcast-qual -Wcast-align -Wpointer-arith -O2 -g #预处理器设置 CPPFLAGS = $(OPTIMIZE)-I。 -Icxxsupport -Ic_utils #默认的Splotch对象 OBJS_SPLOTCH_DEFAULT = cxxsupport / error_handling.o reader / mesh_reader.o cxxsupport / mpi_support.o cxxsupport / paramfile.o \ cxxsupport / string_utils.o cxxsupport / announce.o reader / gadget_reader.o reader / millenium_reader.o \ reader / bin_reader.o reader / tipsy_reader.o splotch / splotchutils.o splotch / scenemaker.o \ cxxsupport / walltimer.o c_utils / walltime_c.o booster / mesh_creator.o booster / randomizer.o \ booster / p_selector.o booster / m_rotation.o cxxsupport / paramfile.o cxxsupport / error_handling.o \ c_utils / walltime_c.o cxxsupport / string_utils.o cxxsupport / announce.o \ cxxsupport / walltimer.o #默认预览器对象 OBJS_PREVIEWER_DEFAULT = main.o预览器/Previewer.o预览器/ libs / core / Parameter.o预览器/ libs / core /ParticleSimulation.o \ 预览器/ libs / core / WindowManager.o预览器/ libs / core / Camera.o预览器/ libs / core / ParticleData.o \ 预览器/ libs / core / MathLib .o previewer / libs / core / FileLib.o previewer / libs / events / OnQuitApplicationEvent.o \ previewer / libs / events / OnKeyReleaseEvent.o previewer / libs / events / OnKeyPressEvent.o previewer / libs / events / OnExposedEvent.o \ 预览器/ libs / events / OnButtonReleaseEvent.o预览器/ libs / events / OnButtonPressEvent.o预览器/ libs / core / Texture.o \ 预览器/ libs / animation / AnimationSimulation。 o #temp强制渲染方法 RENDER_METHOD = FFSDL #当前构建特定对象 ifeq($(RENDER_METHOD),FFSDL) OBJS_BUILD_SPECIFIC = previewer / libs / renderers / FF_DrawList.o预览程序/ libs / materials / FF_ParticleMaterial.o endif #这个构建的所有对象 OBJS = $(OBJS_SPLOTCH_DEFAULT)$(OBJS_PREVIEWER_DEFAULT)$(OBJS_BUILD_SPECIFIC) #规则(注意:构建时会自动删除目标文件) .SUFFIXES:.o .cc .cxx .cpp .cpp.o: $(CXX)-c $(CPPFLAGS)-o$ @$< .cc.o: $(CXX)-c $(CPPFLAGS)-o$ @$ .cxx.o: $(CXX)-c $(CPPFLAGS)-o$ @$ $ b $(EXEC):$(OBJS) $(CXX)$(OBJS)$(LDFLAGS)-o $(EXEC) rm $( OBJS) clean: rm -f $(OBJS) rm -f $(EXEC) 我剪掉了一两个不需要的东西,因此它的一两个位没什么意义(为什么只有一个方法可用的渲染方法选项例如)我是否已经正确编写了规则,有点朦胧,而且这个数字可以解释我的问题吗?虽然它看起来与其他makefile一样,似乎工作,所以我不知道问题是什么。任何人有任何想法?我可以提供更多的信息,如果有必要吗? 解决方案 ,并将它们的定义移入cpp,从链接器错误中移除这些函数。 听起来像是它们不是内联的,在这种情况下您只能在链接程序时使用单一定义。 将 inline 添加到标题中的任何函数定义中解决问题。这放宽了一个定义规则,只要所有定义相同,就允许这些函数在多个翻译单元中定义。 $ b 更新:另外, OBJS_SPLOTCH_DEFAULT 的定义包含重复项; cxxsupport / paramfile.o 会重复,并且可能还有其他的。您需要删除重复项。我建议按照字母顺序保留这样的长列表,以便更容易搜索和发现重复。 So I am trying to use a Makefile to build a project, and im relatively new to makefiles in general. I am getting multiple definition errors when linking for loads of functions, and im pretty sure it is due to my makefile. I cannot post much of the project, as it is pretty large, but the makefile is below, does anything stand out as obviously wrong?I had some functions declared + defined in a header, and moving their definitions into a cpp removed those functions from the linker errors - but I cannot do this for all of them (EDIT: The rest of the functions that are being multiply defined are not in headers, they are in cpp/cc files as standard, saying "i cannot do this for all of them" implied they were all like that, sorry), as a large portion is a codebase I cannot edit. There shouldnt be any errors in the code though as it is building fine in a separate project without my additions (none of which are causing linker errors), so I figure it must be my makefile, but I cant figure out what I am doing wrong. Any Ideas? # Compiler CXX = g++ # Linker settings LDFLAGS = -lGL -lGLU -lXext -lX11 # Executable name EXEC = SplotchPreviewer # Optimizations for compilation OPTIMIZE = -std=c++98 -pedantic -Wno-long-long -Wfatal-errors -Wextra -Wall -Wstrict-aliasing=2 -Wundef -Wshadow -Wwrite-strings -Wredundant-decls -Woverloaded-virtual -Wcast-qual -Wcast-align -Wpointer-arith -O2 -g # Pre-processor settings CPPFLAGS = $(OPTIMIZE) -I. -Icxxsupport -Ic_utils # Default Splotch objects OBJS_SPLOTCH_DEFAULT = cxxsupport/error_handling.o reader/mesh_reader.o cxxsupport/mpi_support.o cxxsupport/paramfile.o \ cxxsupport/string_utils.o cxxsupport/announce.o reader/gadget_reader.o reader/millenium_reader.o \ reader/bin_reader.o reader/tipsy_reader.o splotch/splotchutils.o splotch/scenemaker.o \ cxxsupport/walltimer.o c_utils/walltime_c.o booster/mesh_creator.o booster/randomizer.o \ booster/p_selector.o booster/m_rotation.o cxxsupport/paramfile.o cxxsupport/error_handling.o \ c_utils/walltime_c.o cxxsupport/string_utils.o cxxsupport/announce.o \ cxxsupport/walltimer.o # Default Previewer objects OBJS_PREVIEWER_DEFAULT = main.o previewer/Previewer.o previewer/libs/core/Parameter.o previewer/libs/core/ParticleSimulation.o \ previewer/libs/core/WindowManager.o previewer/libs/core/Camera.o previewer/libs/core/ParticleData.o \ previewer/libs/core/MathLib.o previewer/libs/core/FileLib.o previewer/libs/events/OnQuitApplicationEvent.o \ previewer/libs/events/OnKeyReleaseEvent.o previewer/libs/events/OnKeyPressEvent.o previewer/libs/events/OnExposedEvent.o \ previewer/libs/events/OnButtonReleaseEvent.o previewer/libs/events/OnButtonPressEvent.o previewer/libs/core/Texture.o \ previewer/libs/animation/AnimationSimulation.o #temp force render method RENDER_METHOD = FFSDL # Current build specific objects ifeq ($(RENDER_METHOD),FFSDL) OBJS_BUILD_SPECIFIC = previewer/libs/renderers/FF_DrawList.o previewer/libs/materials/FF_ParticleMaterial.o endif # All objects for this build OBJS = $(OBJS_SPLOTCH_DEFAULT) $(OBJS_PREVIEWER_DEFAULT) $(OBJS_BUILD_SPECIFIC) # Rules (note: object files automatically removed when building) .SUFFIXES: .o .cc .cxx .cpp .cpp.o: $(CXX) -c $(CPPFLAGS) -o "$@" "$<" .cc.o: $(CXX) -c $(CPPFLAGS) -o "$@" "$<" .cxx.o: $(CXX) -c $(CPPFLAGS) -o "$@" "$<" $(EXEC): $(OBJS) $(CXX) $(OBJS) $(LDFLAGS) -o $(EXEC) rm $(OBJS) clean: rm -f $(OBJS) rm -f $(EXEC)Ive cut out one or two unneccesary things, hence one or two bits of it not making much sense (why have a render method option with only one method available for example) Im a bit hazy on whether I have written the rules correctly, and figure this could account for my issue? Although it looks the same as the other makefile which seems to work so Im not sure what the issue is. Anyone have any idea? I can provide more info if necessary? 解决方案 I had some functions declared + defined in a header, and moving their definitions into a cpp removed those functions from the linker errorsThat sounds like they weren't inline, in which case you're only allowed a single definition when linking the program.Add inline to any function definitions in headers to fix the problem. This relaxes the "one definition rule" to allow these functions to be defined in multiple translation units, as long as all the definitions are identical.UPDATE: Also, your definition of OBJS_SPLOTCH_DEFAULT contains duplicates; cxxsupport/paramfile.o is repeated, and there may be others. You'll need to remove the duplicates. I recommend keeping long lists like this in alphabetical order, to make it easier to search and to spot duplicates. 这篇关于建立在GCC上的函数多重定义链接器错误 - Makefile问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 16:09