我需要编写一个工具链文件来在我的构建系统上配置特殊的编译器前缀和CFLAGS。我正试着用CMake做,但我很难过。
以下是我的toolchain.cmake文件的示例:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CROSSCOMPILING TRUE)
set(COMPILER_PREFIX sh4)
set(USER_ROOT_PATH /opt/st40)
set(CMAKE_FIND_ROOT_PATH ${USER_ROOT_PATH})
find_program(CMAKE_C_COMPILER NAMES ${COMPILER_PREFIX}gcc)
find_program(CMAKE_CXX_COMPILER NAMES ${COMPILER_PREFIX}g++)
find_program(CMAKE_AR NAMES ${COMPILER_PREFIX}ar)
find_program(CMAKE_RANLIB NAMES ${COMPILER_PREFIX}ranlib)
set(CMAKE_C_FLAGS "-mruntime=os21 -mboard=board_custom003024_map256_se -specs=lib/custom003024_h205_board.mem")
但是我在编译器检查时遇到了这个错误:
Building C object
CMakeFiles/cmTryCompileExec2889774418.dir/testCCompiler.c.obj
/opt/st40/bin/sh4gcc -o
CMakeFiles/cmTryCompileExec2889774418.dir/testCCompiler.c.obj -c
/project/cmake/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTryCompileExec2889774418
/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec2889774418.dir/link.txt --verbose=1
/opt/st40/bin/sh4gcc
CMakeFiles/cmTryCompileExec2889774418.dir/testCCompiler.c.obj -o
cmTryCompileExec2889774418
sh4gcc: -mboard undefined
gmake[1]: Leaving directory
`/project/cmake/build/CMakeFiles/CMakeTmp'
在CMakeCache.txt上,我可以看到以下变量:
//Flags used by the compiler during all build types.
CMAKE_C_FLAGS:STRING=
如您所见,它不会在sh4gcc调用中使用CMAKE_C_FLAGS变量。我在这里缺少什么?
答案 0 :(得分:6)
我在另一个问题中找到了解决方法。似乎如果我在工具链文件中设置CMAKE_C_FLAGS,如下所示:
set(CMAKE_C_FLAGS "-mruntime=os21 -mboard=board_custom003024_map256_se -specs=lib/custom003024_h205_board.mem" CACHE STRING "" FORCE)
它的工作方式也是我期望的。这似乎没有任何奇怪的副作用。