I want to build an x86 Windows application on my x64 Windows machine.
I use CMake, Ninja, clang-cl, lld-link and the VS Build Tools 2017 with the following CMakeLists
cmake_minimum_required(VERSION 3.9)
project(Test CXX)
add_library(TestLib STATIC "")
target_include_directories(TestLib
PUBLIC
TestLib/inc
)
target_sources(TestLib
PRIVATE
TestLib/src/Flop.cpp
TestLib/src/testClass.cpp
)
add_executable(Test "")
target_sources(Test
PRIVATE
src/main.cpp
)
target_link_libraries(Test
TestLib
)
My setup works fine for x64 applications. I initialise the build environment with vcvars64 and call cmake with
cmake -G Ninja -DCMAKE_CXX_COMPILER:PATH="C:\MeineProgramme\LLVM\bin\clang-cl.exe" -DCMAKE_LINKER:PATH="C:\MeineProgramme\LLVM\bin\lld-link.exe"
This results in perfectly fine ninja build files and produces a functional executable.
If I want to build an x86 application, CMake failes to detect the build environment. I initialise the build environment with vcvars32 or vcvarsamd64_x86 and use the same command as above to invoke CMake. This results in errors during the detection of the build environment.
The CMake error log has the following content
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: C:/MeineProgramme/LLVM/bin/clang-cl.exe
Build flags:
Id flags:
The output was:
1120
LINK : error LNK2001: Nicht aufgelöstes externes Symbol "mainCRTStartup".
C:\MeineProgramme\Visual_Studio\2017\BuildTools\VC\Tools\MSVC\14.11.25503\lib\x86\libcmt.lib : warning LNK4272:Bibliothekcomputertyp "x86" steht in Konflikt mit dem Zielcomputertyp "x64"
CMakeCXXCompilerId.exe : fatal error LNK1120: 1 nicht aufgelöste Externe
clang-cl.exe: error: linker command failed with exit code 1120 (use -v to see invocation)
Determining if the CXX compiler works failed with the following output:
Change Dir: D:/Dateien/Downloads/Test/build/CMakeFiles/CMakeTmp
Run Build Command:"C:/MeineProgramme/Ninja/bin/ninja.exe" "cmTC_e2ed5"
[1/2] Building CXX object CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_e2ed5.exe
FAILED: cmTC_e2ed5.exe
cmd.exe /C "cd . && C:\MeineProgramme\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_e2ed5.dir --manifests -- C:\MeineProgramme\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj /out:cmTC_e2ed5.exe /implib:cmTC_e2ed5.lib /pdb:cmTC_e2ed5.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\MeineProgramme\LLVM\bin\lld-link.exe /nologo CMakeFiles\cmTC_e2ed5.dir\testCXXCompiler.cxx.obj /out:cmTC_e2ed5.exe /implib:cmTC_e2ed5.lib /pdb:cmTC_e2ed5.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_e2ed5.dir/intermediate.manifest CMakeFiles\cmTC_e2ed5.dir/manifest.res" failed (exit code 1) with the following output:
C:\MeineProgramme\LLVM\bin\lld-link.exe: warning: <root>: undefined symbol: mainCRTStartup
error: link failed
ninja: build stopped: subcommand failed.
As far as I understand, the problem is that CMake passes /machine:x64 to the linker.
Which modifications should I make to the CMake invocation or the CMakeLists to solve this problem?
Any help is very much appreciated. Thank you guys :-)
CMake Issue #16259: "Ninja generator fails with clang-cl when mixing 64/32 bit clang and Visual Studio Environment" suggests utilizing environment variables:
The Ninja generator requires that the environment already be configured for the compiler to produce the desired target. You need to do
>set CC=clang-cl
>set CFLAGS=-m32
>set CXX=clang-cl
>set CXXFLAGS=-m32
to tell CMake how to invoke the compiler for the desired target.
Related
I am installing Raspberry Pi Pico SDK using the pre-built installer.
I am getting the error LINK : fatal error LNK1104: cannot open file 'cmTC_9542e.exe'
The same installer is working properly on the different development machine.
Error
Building blink
Microsoft (R) Program Maintenance Utility Version 14.32.31332.0
Copyright (C) Microsoft Corporation. All rights reserved.
Scanning dependencies of target bs2_default
[ 0%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
[ 0%] Linking ASM executable bs2_default.elf
[ 0%] Built target bs2_default
[ 0%] Generating bs2_default.bin
[ 0%] Generating bs2_default_padded_checksummed.S
[ 0%] Built target bs2_default_padded_checksummed_asm
[ 0%] Creating directories for 'ELF2UF2Build'
[ 0%] No download step for 'ELF2UF2Build'
[ 0%] No update step for 'ELF2UF2Build'
[ 0%] No patch step for 'ELF2UF2Build'
[ 0%] Performing configure step for 'ELF2UF2Build'
-- The C compiler identification is MSVC 19.32.31332.0
-- The CXX compiler identification is MSVC 19.32.31332.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx86/x86/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.23/Modules/CMakeTestCCompiler.cmake:69 (message):
The C compiler
"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx86/x86/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Pico/pico-examples/build/elf2uf2/CMakeFiles/CMakeTmp
Run Build Command(s):nmake -f Makefile /nologo cmTC_9542e\fast && "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x86\nmake.exe" -f CMakeFiles\cmTC_9542e.dir\build.make /nologo -L CMakeFiles\cmTC_9542e.dir\build
Building C object CMakeFiles/cmTC_9542e.dir/testCCompiler.c.obj
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_9542e.dir\testCCompiler.c.obj.d --working-dir=C:\Pico\pico-examples\build\elf2uf2\CMakeFiles\CMakeTmp --filter-prefix="Note: including file: " -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1432~1.313\bin\Hostx86\x86\cl.exe #C:\Users\Test\AppData\Local\Temp\nm15C.tmp
testCCompiler.c
Linking C executable cmTC_9542e.exe
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_9542e.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1432~1.313\bin\Hostx86\x86\link.exe /nologo #CMakeFiles\cmTC_9542e.dir\objects1.rsp #C:\Users\Test\AppData\Local\Temp\nm1AC.tmp
FINAL LINK: command "C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1432~1.313\bin\Hostx86\x86\link.exe /nologo #CMakeFiles\cmTC_9542e.dir\objects1.rsp /out:cmTC_9542e.exe /implib:cmTC_9542e.lib /pdb:C:\Pico\pico-examples\build\elf2uf2\CMakeFiles\CMakeTmp\cmTC_9542e.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_9542e.dir/intermediate.manifest CMakeFiles\cmTC_9542e.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'cmTC_9542e.exe'
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "C:/Pico/pico-examples/build/elf2uf2/CMakeFiles/CMakeOutput.log".
See also "C:/Pico/pico-examples/build/elf2uf2/CMakeFiles/CMakeError.log".
NMAKE : fatal error U1077: 'echo' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
C:\Pico>
CMake Error at C:/Program Files/JetBrains/CLion 2019.1.3/bin/cmake/win/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/dbak/Projects/firmware/testing/cmake-build-release-visual-studio/CMakeFiles/CMakeTmp
Run Build Command(s):nmake /nologo cmTC_99064\fast
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe" -f CMakeFiles\cmTC_99064.dir\build.make /nologo -L CMakeFiles\cmTC_99064.dir\build
Building C object CMakeFiles/cmTC_99064.dir/testCCompiler.c.obj
C:\PROGRA~2\MICROS~1\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe #C:\Users\dbak\AppData\Local\Temp\nm9D03.tmp
testCCompiler.c
Linking C executable cmTC_99064.exe
"C:\Program Files\JetBrains\CLion 2019.1.3\bin\cmake\win\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_99064.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\10.0.17763.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\10.0.17763.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\cmTC_99064.dir\objects1.rsp #C:\Users\dbak\AppData\Local\Temp\nm9E1D.tmp
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\cmTC_99064.dir\objects1.rsp /out:cmTC_99064.exe /implib:cmTC_99064.lib /pdb:C:\Users\dbak\Projects\firmware\testing\cmake-build-release-visual-studio\CMakeFiles\CMakeTmp\cmTC_99064.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_99064.dir/intermediate.manifest CMakeFiles\cmTC_99064.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'MSVCRTD.lib'
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2019.1.3\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
CMake will not be able to correctly generate this project.
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.14)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
However, MSVCRTD.lib is located at "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64". So I do not know what I can possibly do. Is this some issue with Clion not having that location in path?
Maybe change architecture to x86?
Where to find architecture setting
I've compiled boost using:
b2.exe link=shared runtime-link=shared address-model=32 variant=release --layout=system --stagedir=./stage/win32
I'm loading boost components to CMake myself, specifically:
find_path(Boost_INCLUDE_DIR NAMES boost/config.hpp PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../external/boost_1_69_0)
message("${Boost_INCLUDE_DIR}")
add_library(Boost INTERFACE IMPORTED GLOBAL)
set_target_properties(Boost PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
find_library(Boost_filesystem_LIBRARY NAMES boost_filesystem PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../external/boost_1_69_0/stage/win32/lib)
message("${Boost_filesystem_LIBRARY}")
add_library(Boost_filesystem UNKNOWN IMPORTED GLOBAL)
set_target_properties(Boost_filesystem PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
set_target_properties(Boost_filesystem PROPERTIES IMPORTED_LOCATION ${Boost_filesystem_LIBRARY})
target_link_libraries(Boost INTERFACE Boost_filesystem)
When invoking CMake:
C:/bla/SW/cmake-template/external/boost_1_69_0
C:/bla/SW/cmake-
template/external/boost_1_69_0/stage/win32/lib/boost_filesystem.lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/bla/SW/cmake-template/build-windows
And last,
Link:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\
bin\HostX86\x86\link.exe /ERRORREPORT:QUEUE /OUT:"C:\bla\SW\cmake-template\build-windows
\mylib\Debug\mylib.dll" /INCREMENTAL /NOLOGO ..\myotherlib\Debug\myotherlib.lib "C:\bla\
SW\cmake-template\external\boost_1_69_0\stage\win32\lib\boost_filesystem.lib" kernel32.lib
user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib
advapi32.lib /DEF:"C:/bla/SW/cmake-template/build-windows/mylib/mylib.dir/Debug/exports.
def" /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PD
B:"C:/bla/SW/cmake-template/build-windows/mylib/Debug/mylib.pdb" /SUBSYSTEM:CONSOLE /TLB
ID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/bla/SW/cmake-template/build-windows/mylib/Debug/
mylib.lib" /MACHINE:X86 /SAFESEH /machine:X86 /DLL mylib.dir\Debug\mylib.obj
LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc141-mt-gd-x32-1_69.lib' [
C:\bla\SW\cmake-template\build-windows\mylib\mylib.vcxproj]
I don't get it, how come it's trying to link to libboost_filesystem-vc141-mt-gd-x32-1_69.lib and not to boost_filesystem.lib?
That link comes from the auto-ink feature of some of the Boost libraries. You can either adjust the auto-link to use the appropriate naming see macros for controlling this in Boost Config library. Or you can turn off the auto-linking with the BOOST_ALL_NO_LIB macro mentioned also in the Boost Config docs.
I try to compile some stuff with Matlab, and during the process Matlab should create some
temporary folders. Unfortunately, for some reason, this is not happening and therefore the compilation fails. I executed Matlab with Administrator rights, but the problems reminds. I am not sure if their is some other problem with the User Access Control under Windows 7. Could anyone give me some instructions of how I can make sure that Matlab has all the privileges to write temporary files?
EDIT:
Actually it is mex file compilation, so the error could also come from the VS compiler:
--> cl /c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD /FoC:\Temp\mex_OmN_AO\test.obj -I"C:\Program Files\MATLAB\R2011a\extern\include" -I"C:\Program Files\MATLAB\R2011a\simulink\include" /O2 /Oy- /DNDEBUG -DMX_COMPAT_32 vcd_count_transitions.c
test.c
Contents of C:\Temp\mex_OmN_AO\mex_tmp.rsp:
C:\Temp\mex_OmN_AO\test.obj
--> link /out:"test.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:"C:\Temp\mex_OmN_AO\templib.x" /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib #C:\Temp\mex_OmN_AO\mex_tmp.rsp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Temp\mex_OmN_AO\test.obj
LINK : fatal error LNK1104: cannot open file 'test.mexw64'
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'test.mexw64
I would expect after the compilation fails, there should be C:\Temp\mex_OmN_AO directory,
but there is nothing in my TEMP folder.
Many thanks for your help!
I have a C-File that I wanna compile in Matlab with MS Visual Studio 10.
For that I use the following command:
mex -v test.c
The output that I get after compilation is the following:
test.c
Contents of C:\Temp\mex_kpuu4t\mex_tmp.rsp:
C:\Temp\mex_kpuu4t\test.obj
--> link /out:"test.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:"C:\Temp\mex_kpuu4t\templib.x" /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib #C:\Temp\mex_kpuu4t\mex_tmp.rsp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Temp\mex_kpuu4t\test.obj
LINK : fatal error LNK1104: cannot open file 'test.mexw64'
C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'test.mexw64' failed.
Anyone an idea what could go wrong here? The interesting thing is, that the test.obj file seems not to be written to the temp folder, could that be the issue here?
Many thanks!
Maybe you called "test" from a previously compiled mex? If yes, it might still be loaded to Matlab's memory, and cannot be overwritten. Try:
clear mex
and then compile again.