Ali Ali - 1 month ago 5
C++ Question

CMake fails in linking

I have a

for my
project that works perfectly on Windows, using
, and on Linux with
. I am trying to also use the same file for cross-compiling on Windows for Linux.

I am using
Eclipse CDT4 - Unix Makefiles
as the generator to create a make file project in Eclipse. In order to use the cross compiler I have created the following toolchain file


set(TOOL_CHAIN_ROOT "C:/mgc/embedded/codebench")

# specify the cross compiler
set(CMAKE_C_COMPILER ${TOOL_CHAIN_ROOT}/bin/x86_64-amd-linux-gnu-gcc.exe)
set(CMAKE_CXX_COMPILER ${TOOL_CHAIN_ROOT}/bin/x86_64-amd-linux-gnu-g++.exe)

and this is how I am generating the project files

cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=../Source/cross-tool.cmake ../Source`

I don't have any problem here. I can even compile the source files with

cmake --build .

The project starts to compile but at the very end when it is trying to link it fails with this error:

[100%] Linking CXX executable crossProject
x86_64-amd-linux-gnu-g++.exe: error: CMakeFiles/crossProject.dir/Path/To/ObjectFile.cpp.o: No such file or directory
make.exe[2]: *** [crossProject] Error 1
make.exe[1]: *** [CMakeFiles/crossProject.dir/all] Error 2
make.exe: *** [all] Error 2

But when I check
I can see the object file that it is complaining it can not find. It's right there.

I have been using this cross-compiler for a while and I am sure it is working properly. I am actually building the same project using the same cross-compiler with Eclipse internal builder without CMake. So the problem should not be with the cross-compiler itself.

Is this normal that it is using the
to link instead of

Ali Ali

The problem is Windows, not the compiler nor CMake. You can read more about it here but the gist of it is that there is a 8192 character limit in Windows command line. Basically you can not run any command longer than 8192 characters. The link command that I CMake was trying to execute has 11353 characters and that's why it fails with that error.

The solution is to use a different shell. As mentioned in the blog post you can download the GNU ARM Eclipse Build Tools from sourceforge.

Once downloaded and installed just copy make.exe and sh.exe from <Installation-Path>/binto wherever your tool chain is and link like a boss!

I've tried it with different compilers, qnx - gcc - cross gcc and it works perfectly.