CMake project compilation error on VisualStudio us

2020-04-27 00:25发布

问题:

I am using VisualStudio CMake project on Windows machine.

I am using an external SDK in order to cross compile my C++ program for Linux arm architecture.

I added the SDK toolchain to cmakeToolchain path which is :

/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake

and this is my CMakeLists.txt file :

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(Boost_USE_STATIC_LIBS ON) 
set(Boost_USE_MULTITHREADED OFF)  
set(Boost_USE_STATIC_RUNTIME OFF)
#list(APPEND CMAKE_PREFIX_PATH /home/ubuntu/linux4sam/poky/build-microchip/tmp/sysroots-components/cortexa5hf-neon/boost/usr/include/boost)
list(APPEND CMAKE_PREFIX_PATH /opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/include)
Set(Boost_NO_BOOST_CMAKE ON)
#set(BOOST_ROOT "/home/ubuntu/linux4sam/poky/build-microchip/tmp/sysroots-components/cortexa5hf-neon/boost")

message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'")
set(CROSS_COMPILER_DIR /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr)
set(CMAKE_C_COMPILER ${CROSS_COMPILER_DIR}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER_DIR}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)

#set(CMAKE_SYSTEM_NAME Linux)

message(STATUS "CMAKE_C_COMPILER='${CMAKE_C_COMPILER}'")
message(STATUS "CMAKE_CXX_COMPILER='${CMAKE_CXX_COMPILER}'")

find_package(Boost 1.66.0) 

if(Boost_FOUND)

    message (STATUS "success!")
    add_executable (CMakeProject4 "CMakeProject4.cpp" "CMakeProject4.h") 
    include_directories(${Boost_INCLUDE_DIRS})  
    target_link_libraries(CMakeProject4 ${Boost_LIBRARIES})

endif()

As you can see I am setting my SDK compilers bin paths to CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables.

When I Debug, I find that :

1> Copying files to the remote machine.
1> Starting copying files to remote machine.
1> [rsync] rsync -t --delete --delete-excluded -v -r --exclude=.vs --exclude=.git --exclude=out  "." rsync://ubuntu@localhost:56934/-home-ubuntu-CMake-RemoteCML
1> [rsync] sending incremental file list
1> [rsync] 
1> [rsync] sent 229 bytes  received 13 bytes  161.33 bytes/sec
1> [rsync] total size is 4,429  speedup is 18.30
1> Finished copying files (elapsed time 00h:00m:04s:043ms).
1> CMake generation started for configuration: 'Linux-Release'.
1> Found cmake executable at /home/ubuntu/.vs/cmake/bin/cmake.
1> /home/ubuntu/.vs/cmake/bin/cmake -G "Unix Makefiles"  -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DBoost_INCLUDE_DIRS:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/include" -DBoost_LIBRARIES:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/lib" -DBoost_INCLUDE_DIR:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/include" -DBoost_DEBUG=ON -DCMAKE_CXX_COMPILER=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -DCMAKE_TOOLCHAIN_FILE="/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake" -DCMAKE_INSTALL_PREFIX=/home/ubuntu/CMake/RemoteIL "/home/ubuntu/CMake/RemoteCML/CMakeLists.txt";
1> [CMake] -- The C compiler identification is GNU 7.4.0
1> [CMake] -- The CXX compiler identification is GNU 7.3.0
1> [CMake] -- Check for working C compiler: /usr/bin/cc
1> [CMake] -- Check for working C compiler: /usr/bin/cc -- works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
1> [CMake] -- Check for working CXX compiler: /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -- broken
1> [CMake] CMake Error at /home/ubuntu/.vs/cmake/share/cmake-3.15/Modules/CMakeTestCXXCompiler.cmake:53 (message):
1> [CMake]   The C++ compiler
1> [CMake] 
1> [CMake]     "/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc"
1> [CMake] 
1> [CMake]   is not able to compile a simple test program.
1> [CMake] 
1> [CMake]   It fails with the following output:
1> [CMake] 
1> [CMake]     Change Dir: /home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeTmp
1> [CMake]     
1> [CMake]     Run Build Command(s):/usr/bin/make cmTC_2cb5a/fast && /usr/bin/make -f CMakeFiles/cmTC_2cb5a.dir/build.make CMakeFiles/cmTC_2cb5a.dir/build
1> [CMake]     make[1]: Entering directory '/home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeTmp'
1> [CMake]     Building CXX object CMakeFiles/cmTC_2cb5a.dir/testCXXCompiler.cxx.o
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc     -o CMakeFiles/cmTC_2cb5a.dir/testCXXCompiler.cxx.o -c /home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
1> [CMake]     Linking CXX executable cmTC_2cb5a
1> [CMake]     /home/ubuntu/.vs/cmake/bin/cmake -E cmake_link_script CMakeFiles/cmTC_2cb5a.dir/link.txt --verbose=1
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc       CMakeFiles/cmTC_2cb5a.dir/testCXXCompiler.cxx.o  -o cmTC_2cb5a 
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crt1.o: No such file or directory
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crti.o: No such file or directory
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtbegin.o: No such file or directory
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc_s
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lc
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find -lgcc_s
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtend.o: No such file or directory
1> [CMake]     /opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/7.3.0/real-ld: cannot find crtn.o: No such file or directory
1> [CMake]     collect2: error: ld returned 1 exit status
1> [CMake]     CMakeFiles/cmTC_2cb5a.dir/build.make:86: recipe for target 'cmTC_2cb5a' failed
1> [CMake]     make[1]: *** [cmTC_2cb5a] Error 1
1> [CMake]     make[1]: Leaving directory '/home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeTmp'
1> [CMake]     Makefile:121: recipe for target 'cmTC_2cb5a/fast' failed
1> [CMake]     make: *** [cmTC_2cb5a/fast] Error 2
1> [CMake]     
1> [CMake] 
1> [CMake]   CMake will not be able to correctly generate this project.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   CMakeLists.txt:6 (project)
1> [CMake] -- Configuring incomplete, errors occurred!
1> [CMake] See also "/home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeOutput.log".
1> [CMake] See also "/home/ubuntu/CMake/RemoteBR/CMakeFiles/CMakeError.log".
1> cd "/home/ubuntu/CMake/RemoteBR";/home/ubuntu/.vs/cmake/bin/cmake -G "Unix Makefiles"  -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DBoost_INCLUDE_DIRS:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/include" -DBoost_LIBRARIES:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/lib" -DBoost_INCLUDE_DIR:PATH="/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/include" -DBoost_DEBUG=ON -DCMAKE_CXX_COMPILER=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -DCMAKE_TOOLCHAIN_FILE="/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake" -DCMAKE_INSTALL_PREFIX=/home/ubuntu/CMake/RemoteIL "/home/ubuntu/CMake/RemoteCML" 2>&1; returned with exit code: 1

I can see here that CMake can identify the SDK toolchain compiler which I want to use which is GNU 7.3.0 but it's broken! When I open CMakeCache.txt , I find this :

//No help, variable specified on the command line.
CMAKE_CXX_COMPILER:UNINITIALIZED=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc

//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_AR:FILEPATH=/usr/bin/gcc-ar-7

//A wrapper around 'ranlib' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_RANLIB:FILEPATH=/usr/bin/gcc-ranlib-7

CMAKE_CXX_FLAGS:STRING=

//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g

//Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG

//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG

//Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

//No help, variable specified on the command line.
CMAKE_TOOLCHAIN_FILE:UNINITIALIZED=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake

This is my C++ .cpp file program :

#include "CMakeProject4.h"
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
    typedef std::istream_iterator<int> in;

    std::cout << "Type in any number: ";

    std::for_each(
        in(std::cin), in(), std::cout
        << (boost::lambda::_1 * 10)
        << "\nType in another number: ");
}

But when I try to run CMake in order to compile my program using :

cmake -DBoost_DEBUG=ON -DCMAKE_CXX_COMPILER=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc

I have this error :

Error Compilation

Any help please ?

回答1:

When you use a toolchain file, you shouldn't set the compiler yourself. You should let it to the toolchain file, as it know how to cross compile. Things like CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR should be left to the toolchain file.

set(Boost_USE_STATIC_LIBS ON) 
set(Boost_USE_MULTITHREADED OFF)  
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_NO_BOOST_CMAKE ON)

# I think the prefix should point to usr directly, not the include directory
list(APPEND CMAKE_PREFIX_PATH /opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr)

message(STATUS "CMAKE_TOOLCHAIN_FILE='${CMAKE_TOOLCHAIN_FILE}'")
message(STATUS "CMAKE_C_COMPILER='${CMAKE_C_COMPILER}'")
message(STATUS "CMAKE_CXX_COMPILER='${CMAKE_CXX_COMPILER}'")

project(whatever CXX)

# Better to fail fast when there's a package missing
find_package(Boost 1.66.0 REQUIRED) 

add_executable (CMakeProject4 CMakeProject4.cpp CMakeProject4.h) 
target_link_libraries(CMakeProject4 PRIVATE Boost::headers)

And your CMake arguments should look like this:

cmake -DCMAKE_TOOLCHAIN_FILE=/opt/poky-atmel/2.5.3/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake