【问题标题】:CMake is able to cross compile on 3rd invocationCMake 能够在第 3 次调用时交叉编译
【发布时间】:2017-05-22 11:50:52
【问题描述】:

我必须交叉编译使用 CMake 的第 3 方库。

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

foo.c:

void main(){}

构建命令:

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

没有“-B 路径”选项,编译器根本无法工作。

因此我在我的 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++)

【问题讨论】:

  • 你能显示你使用的所有工具链文件吗?
  • 当然,我编辑了问题。
  • 我想知道在这种情况下您是否应该使用CMAKE_FORCE_XXX_COMPILER() 宏,因为您需要添加这些标志...在这里查看cmake_cross_compiling 并查找INCLUDE(CMakeForceCompiler)
  • 正是:) 我使用了以下链接中提到的方法:stackoverflow.com/questions/6476203/… 非常感谢您的提示。我不确定您是否应该将其添加为答案,或者该问题应作为重复项关闭。
  • 嗯,好吧,我没看到,我只是阅读了我链接的文档......我将在答案中包含这些链接(文档和你提到的这个 SO)。

标签: cmake cross-compiling


【解决方案1】:

看来捡旗子的问题和之前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)  
...

与第一次破解的效果相同。第二个选项似乎是一个更清洁的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多