gpt4 book ai didi

c++ - 如何在实践中解决项目编译中链接库的顺序

转载 作者:行者123 更新时间:2023-11-30 17:07:55 24 4
gpt4 key购买 nike

我一直在从事一个项目,该项目使用来自许多作者(物理学)的不同来源和代码,我想将它们合并在一起并在它们之间进行通信。问题是其中一些源代码和 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++ Linker errors: Undefined reference to 'vtable for XXX', Undefined reference to 'ClassName::ClassName()'

Why does the order in which libraries are linked sometimes cause errors in GCC?

Why am I getting a gcc "undefined reference" error trying to create shared objects?

所以,我有两个问题:

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

如何将此代码分发给其他人,而不告诉他们更改在 C 文件之前调用库的所有 makefile?

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

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

我对 makefile 的经验很少。

最佳答案

在我的个人生活中,我使用自己的 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

关于c++ - 如何在实践中解决项目编译中链接库的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949615/

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