【问题标题】:Create an executable out of linking only with CMake仅通过 CMake 链接创建可执行文件
【发布时间】:2019-12-19 09:34:55
【问题描述】:

我希望 CMake 在生成我的 kernel 可执行文件时发出以下命令或等效命令

mipsel-linux-gnu-ld -o kernel hello.o termprint.o crtso.o libumps.o -G 0 -nostdlib -T ./umps/umpscore.ldscript

请注意,上述命令完全跳过了对kernelgcc 调用——它只运行链接器。为了使用 CMake 复制该命令,我正在尝试定义以下规则

file(WRITE null.c "")

add_executable(kernel null.c)
target_link_options(kernel PRIVATE ${LDFLAGS})
target_link_libraries(kernel phase0 crtso libumps termprint)

但问题是生成的Makefile 发出一个编译步骤来生成kernel,而我只想做链接

$ make VERBOSE=1 kernel

# Lots of output
...
mipsel-linux-gnu-gcc   -rdynamic -G 0 -nostdlib -T /home/acsor/Software/BiKaya/src/umps/umpscore.ldscript CMakeFiles/kernel.dir/null.c.o CMakeFiles/phase0.dir/src/phase0.c.o CMakeFiles/crtso.dir/src/umps/crtso.S.o CMakeFiles/libumps.dir/src/umps/libumps.S.o CMakeFiles/termprint.dir/src/termprint.c.o  -o kernel
...

# Link errors follow

如何以 CMake 风格的方式做我想做的事?你可以找到我的 CMake 设置here

【问题讨论】:

  • 编译步骤在哪里? GCC 也可以用于链接。
  • hello.o termprint.o crtso.o libumps.o 是否也由 CMake 文件编译?请同时显示 CMake。
  • Here it is。我正在考虑调整 CMAKE_C_LINK_EXECUTABLE 变量,有什么缺点吗?
  • 这些.c.o 扩展名都正确吗?...
  • @Acsor 也许最干净的方法是创建一个工具链文件?看看here

标签: c assembly cmake


【解决方案1】:

无需为每个 C 文件创建单独的对象库。只需执行以下操作:

add_executable(kernel ${SRC}/phase0.c ${UMPS_SRC}/crtso.S ${UMPS_SRC}/libumps.S ${SRC}/termprint.c)
set_target_property(TARGET kernel APPEND PROPERTY LINK_FLAGS -rdynamic -G 0 -nostdlib -T ${UMPS_SRC}/umpscore.ldscript) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多