简体   繁体   中英

CMake Passed variables compiler is not able to compile a simple program

I am trying to cross compile an application using CMake. I have everything working when I hardcode the paths to the cross compiler. However, when I pass in the paths via command line arguments, it seems to not track them properly.

Here is my shell script, from which I clone a repo, apply a patch, and then attempt a cmake build:

   cmake -D BASE=$PATH_TO_TOOLCHAIN_ROOT -D BOOST_ROOT=$BOOST_ROOT -D CMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../${GIT_PATH}/src/

Here is my toolchain.cmake file:

set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_VERSION 1 )
set(PKG_CONFIG_EXECUTABLE "/usr/bin/pkg-config")

message("Base: ${BASE}")
message("Boost: ${BOOST_ROOT}")

set(CMAKE_SYSROOT "${BASE}/sysroot")

set(CMAKE_C_COMPILER "${BASE}/sysroot/usr/bin/mipsel-linux-gcc")
set(CMAKE_CXX_COMPILER "${BASE}/sysroot/usr/bin/mipsel-linux-g++") 

set( CMAKE_FIND_ROOT_PATH "${BASE}/sysroot")

set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
# for libraries and headers in the target directories
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)


include_directories(${CMAKE_SYSROOT}/usr/local/include/)

set(BOOST_INCLUDEDIR ${BOOST_ROOT}/include)
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/lib)

include_directories(${BOOST_INCLUDEDIR})

set(OPENSSL_ROOT_DIR ${BASE}/sysroot/usr/lib/)
set(OPENSSL_SSL_LIBRARY libssl.so)
set(OPENSSL_CRYPTO_LIBRARY libcrypto.so)

Again, if I hardcode ${BASE} in this toolchain file as /path/to/mipsel-linux-gcc, it works.

However, when I actually call this, I get this printout:

+ cmake -D BASE=/path/to/toolchain/root -D BOOST_ROOT=/home/matthew/boost/out -D CMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../build/src/
Base: /path/to/toolchain/root
Boost: /home/matthew/boost/out
Base: /path/to/toolchain/root
Boost: /home/matthew/boost/out
-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc
Base: 
Boost: 
-- Check for working C compiler: /path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc -- broken
CMake Error at /usr/local/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
  The C compiler
  "/path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc"
  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: /home/matthew/Desktop/clones/build/CMakeFiles/CMakeTmp

Here you can see that it clearly does find the compilers, and identifies them both as GNU 4.7.3. However, notice the printouts Base: /path/to/toolchain/root . This message() directive was only listed one time in toolchain.cmake, and yet it prints out several times here, and eventually shows as a blank variable. After that, it complains that the C compiler doesn't work, even though it clearly then prints out the full path to that compiler.

I have seen similar problems of other people, but this seems different because it works fine when I hardcode the path in toolchain.cmake to the C and CXX compiler. I have tried with quotes around the C and CXX compiler directive, as well as without.

I am also interested in learning why my message directives print out so many times!

Thanks

The CMake is doing a test build to find out if the compiler is able to build a simple app (basically just containing main() ). This might fail for multiple reasons, for example the compiler does not work with default CMake detected flags, etc.

You should check the CMakeError.log and CMakeOutput.log to see why the test compile failed.

To skip the compiler test, you might as well put the following to the toolchain file:

set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

This will skip the compiler test, but of course the compilation might then fail later when you actually build your project.

Check my reply here:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207460115/comments/360002077699

Basically cmake during its system introspection will generate some files (test C/CXX files, Makefiles and even a new CMake project) through the try_compile command and then delete them by default.

If you want to be able to disable the deletion of these generated files and debug your issue you can add --debug_trycompile while running cmake and you will find some very useful information in root/cmake_build/CMakeFiles/CMakeTmp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM