如何在实践中解决项目编译中链接库的顺序问题

How to solve in practice the order of linked libraries in compilation for a project

本文关键字:链接 顺序 问题 编译 实践中 解决 项目      更新时间:2023-10-16

我一直在做一个项目,该项目使用了许多作者(物理学)的不同来源和代码,我想将它们合并在一起,并在它们之间进行通信。问题是,其中一些源代码和makefile首先调用链接库,然后调用c文件:

$(CC) $(lflags) -o smith2demo smith2demo.o smith2.o

直到现在,在我所在研究所的计算机和其他一些系统中,一切都很正常。我有一个gcc编译器:

$gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.

所以,直到我尝试在Ubuntu:上运行我的代码,我才注意到这个问题

gcc (Ubuntu 4.9.3-5ubuntu1) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.

在ubuntu中,我得到了类似的东西:

smith2.c:(.text+0x29b): undefined reference to `sincos'

我知道链接库的规范和原因,在这里回答:

GCC C++链接器错误:对';vtable for XXX';,未定义对';ClassName::ClassName()';

为什么库的链接顺序有时会导致GCC中的错误?

为什么我得到了一个gcc"未定义引用";尝试创建共享对象时出错?

所以,我有两个问题:

如果两个gcc都是最新版本,为什么我在Debian系统中没有这个问题?

如果我不告诉其他人更改在C文件之前调用库的所有makefile,我该如何将这些代码分发给其他人?

在我的项目中的大多数情况下,我使用一个完整的Makefile,然后我只更改到源文件夹并在其中执行$(MAKE)

有没有一种方法可以将--no-as-needed设置为每个人的选项,或者更智能的方法?

我对makefiles没有什么经验。

在我的个人生活中,我使用自己的Makefile。这是它的简单版本。

    MAIN = main
    HEADER_DEFINITIONS = fibo
    CC = g++-4.9 -std=c++11
    COMPILE = -c
    EXE = $(MAIN)
    OPTIMIZE = -Os
    SHELL = /bin/bash
    ARGS = 20
    all: link
    @echo "Executing..........."
    @echo " > > > > > > OUTPUT < < < < < < "
    @$(SHELL) -c './$(EXE) $(ARGS)'
    link: compile
    @echo -n "Linking............."
    @$(SHELL) -c '$(CC) -o $(EXE) *.o'
    compile: $(MAIN).cpp $(HEADER_DEFINITIONS).cpp
    @echo -n "Compiling........."
    @$(SHELL) -c '$(CC) $(OPTIMIZE) $(COMPILE) $^'
    clean:
    @echo "Cleaning............"
    @$(SHELL) -c 'rm -f *~ *.o $(EXE)'

如果你想进一步修改和添加某些链接器标志,是完全可能的

编辑2我的个人Makefile

#
# A simple makefile for managing build of project composed of C source files.
#

# It is likely that default C compiler is already gcc, but explicitly
# set, just to be sure
CC = gcc
# The CFLAGS variable sets compile flags for gcc:
#  -g        compile with debug information
#  -Wall     give verbose compiler warnings
#  -O0       do not optimize generated code
#  -std=c99  use the C99 standard language definition
#  -m32      CS107 targets architecture IA32 (32-bit)
CFLAGS = -g -Wall -O0 -std=c99 -m32
# The LDFLAGS variable sets flags for linker
#  -lm   says to link in libm (the math library)
LDFLAGS = -lm
# In this section, you list the files that are part of the project.
# If you add/change names of source files, here is where you
# edit the Makefile.
SOURCES = demo.c vector.c map.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = demo

# The first target defined in the makefile is the one
# used when make is invoked with no argument. Given the definitions
# above, this Makefile file will build the one named TARGET and
# assume that it depends on all the named OBJECTS files.
$(TARGET) : $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

.PHONY: clean
clean:
    @rm -f $(TARGET) $(OBJECTS) core