4
votes

I've a problem to force CMake to use the MSVS linker instead of the MinGW linker. To support MSVC in MSYS I've implement a bash script forcing CMake to use the MSVC compiler to create NMake files:

cmake -G "NMake Makefiles" -DMSVC=true -DCOMPILER_ROOT=$COMPILER_ROOT -DCMAKE_PROFILE=$CMAKE_PROFILE -DCMAKE_BUILD_TYPE=$CMAKE_DEBUG -DCMAKE_CXX_LINKER=$COMPILER_ROOT/bin/linker.exe

CMake uses the MSVC compiler correctly and creates the NMake files as expected:

$ sh build.sh -msvc /c/binrev/development/vs2010/VC -p   
removing CMakeCache.txt
removing temporary directory CMakeFiles
Create MSVC based NMake files.
-- The C compiler identification is MSVC
-- The CXX compiler identification is MSVC
-- Check for CL compiler version
-- Check for CL compiler version - 1600
-- Check if this is a free VC compiler
-- Check if this is a free VC compiler - no
-- Check for working C compiler: c:/binrev/development/vs2010/VC/bin/cl.exe
-- Check for working C compiler: c:/binrev/development/vs2010/VC/bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: c:/binrev/development/vs2010/VC/bin/cl.exe
-- Check for working CXX compiler: c:/binrev/development/vs2010/VC/bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
Execute: brCore.cmake 
Linker path CMAKE_CXX_LINKER: c:/binrev/development/vs2010/VC/bin/link.exe

But when invoking nmake, CMAKE still uses the MinGW linker:

Linking CXX shared library bin\brCore.dll
c:\binrev\development\mingw\bin\ld.exe: unrecognized option '-Wl,--enable-auto-import'
c:\binrev\development\mingw\bin\ld.exe: use the --help option for usage information
LINK failed. with 1
NMAKE : fatal error U1077: "c:\binrev\development\cmake\bin\cmake.exe": Rückgabe-Code  "0xffffffff"
Stop.
NMAKE : fatal error U1077: "c:\binrev\development\vs2010\VC\BIN\nmake.exe": Rückgabe-Code "0x2"
Stop.
NMAKE : fatal error U1077: "c:\binrev\development\vs2010\VC\BIN\nmake.exe": Rückgabe-Code "0x2"
Stop.

After invoking the bash shell script in CMakeCache the MinGW linker is set instead of MSVC linker:

//Path to a program.
CMAKE_LINKER:FILEPATH=c:/binrev/development/mingw/bin/ld.exe

Ok I've figured out that I've to set the CMAKE_LINKER variable instead of CMAKE_CXX_LINKER to get the correct dependency. But now i got another failure:

Linking CXX shared library bin\brCore.dll
Das System kann die angegebene Datei nicht finden
mt.exe : general error c10100b1: Failed to load file "bin\brCore.dll". Das System kann  die angegebene Datei nicht finden.
MT failed. with 31

Has anyone an idea what I've made wrong? Thanks for any help.

Best regards, Hellhound

1
Do you have to run this build in the MSYS shell? Why not use a "Visual Studio Command Prompt" as the shell if you are using the MSVC compiler and nmake anyway...? - DLRdave
I've run this in MSYS shell while I use it also for GCC. I will try out the VS command promt as shell. - Hellhound
I've tried to run this without using the MSYS shell by executing the cmake script from Visual Studio Command Promt. Same effect, the linker tries to use the MinGW linker ... - Hellhound

1 Answers

0
votes

This sounds like a classic "more than one thing with the same name" in the PATH problem, with the "wrong" one being in the PATH first.

If you put the directory with the MS tools at the front of the PATH, do you still get the same sort of problem?

If you type "which link" in your MSYS bash prompt, what file is first in the environment?

The tools from Microsoft are probably not robust against other tools with the same names being in the PATH. (i.e. -- perhaps mt.exe is simply calling "link.exe" and expecting it to be in the PATH, and does not fully qualify it to ensure the link.exe it needs...) Similarly for the MSYS tools.

You really have to set up your build environments carefully to avoid problems like this when you have multiple development environments installed on a single machine.