如何在实践中解决项目编译中链接库的顺序问题
How to solve in practice the order of linked libraries in compilation for a project
我一直在做一个项目,该项目使用了许多作者(物理学)的不同来源和代码,我想将它们合并在一起,并在它们之间进行通信。问题是,其中一些源代码和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
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- libavdevice 和 x11 的静态链接库应该以什么顺序链接?
- 链接库时的默认目录上的GCC或G 路径搜索顺序
- 编写链接函数的更短/更有效的方法,该函数按字典顺序添加新元素
- 双链表中按字母顺序排序的链接
- 为什么更改链接顺序可以修复一个系统上的一些链接错误?
- 链接列表并按字母顺序插入字符串
- 队列顺序中的链接列表
- AC_LANG_PROGRAM由于链接器选项的顺序失败了链接阶段
- 如何确定最快的链接顺序
- 通过Microsoft Visual Studio 2010 IDE 进行精确的库链接顺序控制
- 没有调用C++析构函数,具体取决于链接顺序
- 不同库中的相同符号和链接顺序
- 库的链接顺序问题
- 如何在忽略链接顺序的情况下链接源
- 为什么使用内联函数的程序根据链接顺序和参数具有不同的行为
- 绕过 gcc 中的静态链接顺序问题
- 在Qt Android项目中使用OpenCV时的链接顺序
- 是否可以在 cmake 中更改库链接顺序
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确