CMAKE单独的链接器和编译器标志

时间:2013-05-16 12:57:07

标签: eclipse cmake

现在我的toolchain.cmake看起来很喜欢这个:

CMAKE_FORCE_C_COMPILER(arm-none-eabi-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(arm-none-eabi-gcc GNU)
SET(CMAKE_ASM_COMPILER arm-none-eabi-as)
SET(CMAKE_OBJCOPY arm-none-eabi-objcopy CACHE INTERNAL "objcopy" FORCE)
SET(CMAKE_OBJDUMP arm-none-eabi-objdump CACHE INTERNAL "objdump" FORCE)

# Compiler Flags 
SET(MCU_FLAGS "-mcpu=cortex-m3 -mthumb")
SET(CMAKE_BUILD_FLAGS "-O2 -g -mfpu=vfp -mfloat-abi=soft -Wa,-meabi=5 -ffunction-sections -fno-common -pedantic -Wall -Wextra -Wno-missing-field-initializers")
SET(CMAKE_C_FLAGS "${MCU_FLAGS} ${CMAKE_BUILD_FLAGS}"  CACHE INTERNAL "c compiler flags")
SET(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS} CACHE INTERNAL "cxx compiler flags")
SET(CMAKE_ASM_FLAGS "-g -mcpu=cortex-m3 -mthumb -mfpu=vfp -mfloat-abi=soft -meabi=5 -warn" CACHE INTERNAL "asm compiler flags")

SET(CMAKE_EXE_LINKER_FLAGS "-mcpu=cortex-m3 -mthumb -Wall -Wextra -Wno-missing-field-initializers -g -nostartfiles -Wl,-T1.ld,-M=1.map,--gc-sections" CACHE INTERNAL "exe link flags")

问题是CMake还向Linker传递了编译器标志!有没有办法把这两个分开?

另一件事是,如果我使用另一个编译器工具链(如Fujitsu Softtune),我怎么能明确告诉CMake采用链接器而不是编译器来链接对象?

由于


所以我终于解决了它。对于具有相同问题的evryone,我改变了CMakelists如下:

add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C CXX ASM)
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES  LINK_FLAGS "${LD_FLAGS_STR} -m ${PROJECT_NAME}.map")
set(CMAKE_C_LINK_EXECUTABLE "FLNK907S.EXE  -o <TARGET> <LINK_FLAGS> <OBJECTS>")

它工作正常......

其中${LD_FLAGS_STR}只是我的链接器标志列表(自己的变量)。 LNK907S.EXE是我正在使用的链接器(Fujitsu)的名称。

2 个答案:

答案 0 :(得分:1)

尝试使用SET_TARGET_PROPERTIES分别设置链接标志:

SET(MCU_FLAGS "-mcpu=cortex-m3 -mthumb")
SET(CMAKE_BUILD_FLAGS "-O2 -g -mfpu=vfp -mfloat-abi=soft -Wa,-meabi=5 -ffunction-sections -fno-common -pedantic -Wall -Wextra -Wno-missing-field-initializers")
SET(CMAKE_C_FLAGS "${MCU_FLAGS} ${CMAKE_BUILD_FLAGS}")
SET(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
SET(CMAKE_ASM_FLAGS "-g -mcpu=cortex-m3 -mthumb -mfpu=vfp -mfloat-abi=soft -meabi=5 -warn")
SET_TARGET_PROPERTIES(FOO PROPERTIES LINK_FLAGS "-mcpu=cortex-m3 -mthumb -Wall -Wextra -Wno-missing-field-initializers -g -nostartfiles -Wl,-T1.ld,-M=1.map,--gc-sections")

在这里,我假设你的excutable被称为“FOO”。请注意,链接库是基于每个目标指定的。如果您只需要针对目标的特殊编译器标志,那么您也可以在COMPILE_FLAGS中使用SET_TARGET_PROPERTIES

答案 1 :(得分:0)

对于较新版本的cmake,我首选使用生成器表达式[1]。

typedef (double)FUN(const double *);
double api(const double *arr, FUN callback){
    return FUN(arr);
}

[1] https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html