gpt4 book ai didi

c - 如何在CMake中添加.h/.o已编译的库

转载 作者:行者123 更新时间:2023-11-30 19:08:33 26 4
gpt4 key购买 nike

似乎是一个简单的问题,但我似乎找不到任何关于此的资源......

我的项目树很简单:main.c 包括 list.h,最后。

cmake_minimum_required(VERSION 3.7)
project(as03)

set(CMAKE_C_STANDARD 11)

set(SOURCE_FILES main.c list.h)

add_executable(as03 ${SOURCE_FILES} list.o)

此 CMakeLists.txt 文件出现错误:

/home/.../clion-2017.1.2/bin/cmake/bin/cmake --build /home/shawn/CLionProjects/CMPT300/as03/cmake-build-debug --target all -- -j 4
[ 50%] Linking C executable as03
/usr/bin/ld: ../list.o: relocation R_X86_64_32S against undefined symbol `headlist' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
CMakeFiles/as03.dir/build.make:96: recipe for target 'as03' failed
make[2]: *** [as03] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/as03.dir/all' failed
make[1]: *** [CMakeFiles/as03.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
<小时/>

编辑:环境是 Linux 上的 CLion IDE,带有 gcc 6.3.0。

编辑2:我很抱歉,

relocation R_X86_64_32S against undefined symbol `headlist' can not be used when making a shared object

错误不是 CMake 的问题,但可以通过相应 makefile 的命令行编译来重现:

CC=gcc
CFLAGS=-w -std=c11
PROG=simulation-app
OBJS= main.o list.o

simulation-app: $(OBJS)
$(CC) $(CFLAGS) -o $(PROG) $(OBJS)

main.o: main.c
$(CC) $(CFLAGS) -c main.c


clean:
ls | grep -v list.o | grep .o | xargs rm

事实证明,我的一位同行遇到了这个问题,并通过重新安装新的 linux 发行版解决了...不,谢谢。

所以这似乎是一个与机器相关的问题。我尝试将 gcc 版本降级到 5.4.1 以匹配编译该 .o 的机器,但没有任何改变:(

最佳答案

add_executable(as03 main.c list.c)

您应该只在目标之后使用列表中的源,而不是目标文件。

除非您使用或想要支持 IDE(尤其是 Visual Studio),否则您也不需要列表中的 header 。

关于c - 如何在CMake中添加.h/.o已编译的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002431/

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