CMake能够在第3次调用

时间:2017-01-07 22:36:23

标签: cmake cross-compiling

我要交叉编译使用CMake的第三方库。

为了构建某些东西,我需要将“-B path”标志传递给编译器。

foo.c:

void main(){}

构建命令:

cross-gcc -B /path/to/somewhere -o foo.o foo.c

如果没有“-B path”选项,编译器就无法工作。

因此我在我的cmake工具链文件中使用以下行:

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

但是在编译器验证期间,CMake似乎没有使用我设置的标志:

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- broken
    CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "cross-path/cross-gcc" is not able
  to compile a simple test program.

....
....

/cross-path/cross-gcc -o CMakeFiles/cmTryCompileExec.dir/testCCompiler.c.o -c CMakeFiles/CMakeTmp/testCCompiler.c

当我第二次调用CMake时,cross-gcc通过了简单的编译测试,但这次cross-g ++失败了:

-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /cross-path/cross-gcc
-- Check for working C compiler: /cross-path/cross-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /cross-path/cross-g++
-- Check for working CXX compiler: /cross-path/cross-g++ -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake:45 (MESSAGE):
  The C++ compiler "/cross-path/cross-g++" is not
  able to compile a simple test program.

....
....

  /cross-path/cross-g++ -o CMakeFiles/cmTryCompileExec.dir/testCXXCompiler.cxx.o -c CMakeFiles/CMakeTmp/testCXXCompiler.cxx

如果我第三次调用CMake,一切正常,只有一个警告:

....
CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_TOOLCHAIN_FILE

我正在使用CMake 2.8.7,并且知道它已经过时但如果是这种情况则升级不是一种选择。

编辑:

工具链文件:

# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

# specify the cross compiler
SET(CMAKE_C_COMPILER   /cross-path/cross-gcc)
SET(CMAKE_CXX_COMPILER /cross-path/cross-g++)

1 个答案:

答案 0 :(得分:3)

看起来拿起旗帜的问题与过去在SO中已经讨论过的问题非常类似:
CMake cross-compiling: C flags from toolchain file ignored
尝试提出的解决方案,即替换:

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

通过

SET(CMAKE_C_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere" CACHE STRING "" FORCE)

还要检查是否更换了使用INCLUDE(CMakeForceCompiler)的新工具链文件,如上所述 http://www.vtk.org/Wiki/CMake_Cross_Compiling
How can I make Cmake use specific compiler and flags when final compilation stage instead of detection?
Cmake cross compile flags

INCLUDE(CMakeForceCompiler)
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_FLAGS "-B /path/to/somewhere")
SET(CMAKE_CXX_FLAGS "-B /path/to/somewhere")

# specify the cross compiler
CMAKE_FORCE_C_COMPILER(/cross-path/cross-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(/cross-path/cross-g++ GNU)  
...

与第一次黑客具有相同的效果。第二种选择似乎更清晰。