【问题标题】:How to pass a compile flag to cmake initial compiler test?如何将编译标志传递给 cmake 初始编译器测试?
【发布时间】:2012-10-12 08:26:27
【问题描述】:

我正在尝试使用 CMake 构建一个项目,该项目将 MSPGCC cross-compiler 用于 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)使其不会失败?

【问题讨论】:

    标签: cmake cross-compiling msp430


    【解决方案1】:

    根据文档:

    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) 不幸的是没有效果。
    • 不只是包含,还有两行 CMAKE_FORCE_C_COMPILER(arm-elf-gcc GNU) CMAKE_FORCE_CXX_COMPILER(arm-elf-g++ GNU) - 你也有吗?
    • 是的,就是这样!我没有注意到 CMAKE_FORCE_C_COMPILER 实际上代替了 CMAKE_C_COMPILER。这确实会覆盖编译器测试,而不是让它使用 -mmcu 开关,对吧?谢谢!
    • 这个答案已经过时了。请看下面的my answer
    【解决方案2】:

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

    工具链文件应在适当的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 variables documentation 中找到,但完整列表(在撰写本文时)为:

    • 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 documentation中找到。

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2019-04-14
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      相关资源
      最近更新 更多