如何向CMake初始编译器测试传递编译标志?

10

我正在尝试使用CMake构建一个项目,该项目使用MSPGCC交叉编译器为MSP430微控制器编译。为了成功编译任何简单程序,我们需要传递一个编译标志,指示目标处理器,否则它会失败,就像这样:

$ msp430-gcc -o test test.c
In file included from test.c:1:0:
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/include/msp430.h:813:2: warning: #warning Unable to identify and include MCU header, use -mmcu=MCU [-Wcpp]
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open linker script file memory.x: No such file or directory
collect2: ld returned 1 exit status
因此,如果我使用 -mmcu 开关指定处理器,它可以正常工作。问题是,尽管我已经在我的CMakeLists.txt文件中指定了这个处理器:
cmake_minimum_required (VERSION 2.6)

project (test-project C)

set (SOURCES
  test.c
  )

add_executable (test-project ${SOURCES})
set (CPU_FLAG "-mmcu=msp430f148")

set (CMAKE_C_FLAGS ${CPU_FLAG})
set (CMAKE_EXE_LINKER_FLAGS ${CPU_FLAG})

CMake抱怨编译器未能通过编译一个简单的程序的测试,我猜是因为它可能没有使用-mmcu开关(请注意无法打开链接脚本文件memory.x的消息):

$ cd ~/git/test-project
$ mkdir build
$ cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../msp430.cmake ..
-- The C compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/local/bin/msp430-gcc
-- Check for working C compiler: /usr/local/bin/msp430-gcc -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "/usr/local/bin/msp430-gcc" is not able to compile a simple
  test program.

  It fails with the following output:

   Change Dir: /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp



  Run Build Command:/usr/bin/gmake "cmTryCompileExec2889462763/fast"

  /usr/bin/gmake -f CMakeFiles/cmTryCompileExec2889462763.dir/build.make
  CMakeFiles/cmTryCompileExec2889462763.dir/build

  gmake[1]: Entering directory
  `/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'

  /usr/bin/cmake -E cmake_progress_report
  /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/CMakeFiles 1

  Building C object
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o

  /usr/local/bin/msp430-gcc -o
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -c
  /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/testCCompiler.c

  Linking C executable cmTryCompileExec2889462763

  /usr/bin/cmake -E cmake_link_script
  CMakeFiles/cmTryCompileExec2889462763.dir/link.txt --verbose=1

  /usr/local/bin/msp430-gcc
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -o
  cmTryCompileExec2889462763 -rdynamic

  /usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open
  linker script file memory.x: No such file or directory

  collect2: ld returned 1 exit status

  gmake[1]: Leaving directory
  `/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'

  gmake[1]: *** [cmTryCompileExec2889462763] Error 1

  gmake: *** [cmTryCompileExec2889462763/fast] Error 2





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)

-- Configuring incomplete, errors occurred!

仅供参考,我的工具链文件如下,我的PATH变量允许它在/usr/local/bin中找到编译器二进制文件:

# the name of the target operating system
#SET(CMAKE_SYSTEM_NAME Linux)

# which C and C++ compiler to use
SET(CMAKE_C_COMPILER   msp430-gcc)
SET(CMAKE_CXX_COMPILER msp430-g++) 

# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /usr/local/msp430) 

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search 
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

尽管如此,有人可以告诉我如何检查CMake正在使用哪些编译标志来执行编译器测试,以及我们如何传递自定义标志(例如-mmcu),以便它不会失败吗?

2个回答

6
根据文档:http://www.cmake.org/Wiki/CMake_Cross_Compiling#The_toolchain_file,你应该使用 CMakeForceCompiler。
INCLUDE(CMakeForceCompiler)

# this one is important
SET(CMAKE_SYSTEM_NAME eCos)

# specify the cross compiler
CMAKE_FORCE_C_COMPILER(arm-elf-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(arm-elf-g++ GNU)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH  /home/alex/src/ecos/install )

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

我在我的MSP430上也使用它,一切正常。


当我使用MSPGCC4时,它也运行得很好,但现在我正在使用更新的MSPGCC时,编译器测试失败了(MSPGCC4已停止开发,并将开发转回原始项目)。 INCLUDE(CMakeForceCompiler)不幸地没有效果。 - Claudio
2
不仅是包含文件,还有两行 CMAKE_FORCE_C_COMPILER(arm-elf-gcc GNU) 和 CMAKE_FORCE_CXX_COMPILER(arm-elf-g++ GNU) - 你也有它们吗? - duselbaer
是的,就是这样!我没有注意到CMAKE_FORCE_C_COMPILER实际上代替了CMAKE_C_COMPILER。这确实覆盖了编译器测试,而不是使用-mmcu开关,对吧?谢谢! - Claudio
这个答案已经过时了。请查看下面的我的回答 - Alex Reinking

4
这里的另一个回答已经过时了。

工具链文件应该在适当的CMAKE_<KIND>_FLAGS(_<CONFIG>)_INIT变量中包含所需的标志,如下所示:

set(CMAKE_C_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_CXX_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-mmcu=msp430f148")

CMake的编译器检测程序将在编译测试可执行文件时使用这些标志。完整列表可在CMake变量文档中找到,但完整列表(截至撰写本文时)如下:
  • CMAKE_<LANG>_FLAGS_<CONFIG>_INIT
  • CMAKE_<LANG>_FLAGS_INIT
  • CMAKE_EXE_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_EXE_LINKER_FLAGS_INIT
  • CMAKE_MODULE_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_MODULE_LINKER_FLAGS_INIT
  • CMAKE_SHARED_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_SHARED_LINKER_FLAGS_INIT
  • CMAKE_STATIC_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_STATIC_LINKER_FLAGS_INIT
其中<CONFIG>是任何全大写配置名称,包括但不限于DEBUGRELWITHDEBINFOMINSIZERELRELEASE。非<CONFIG>变体适用于所有配置,并由<CONFIG>变体增强
<LANG>是任何enable_language()所知的语言之一,目前包括CXXCCUDAOBJCOBJCXXFortranHIPISPCASM。当前列表可以在enable_language文档中找到

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接