clang error: linker command failed with exit code 1104 - c

Hi I'm rather new to Windows command prompt(have only been using Linux-based OS)
I'm trying to install OpenBLAS for Windows according to this tutorial.
When I tried running step 6
cmake .. -G "Ninja" -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_C_COMPILER=clang-cl -DCMAKE_Fortran_COMPILER=flang -DBUILD_WITHOUT_LAPACK=no -DNOFORTRAN=0 -DDYNAMIC_ARCH=ON -DCMAKE_BUILD_TYPE=Release
I get the following error
Compiling the C compiler identification source file
"CMakeCCompilerId.c" failed. Compiler:
C:/ProgramData/Anaconda3/Library/bin/clang-cl.exe Build flags:
/DWIN32;/D_WINDOWS;/W3 Id flags:
The output was: 1104 LINK : fatal error LNK1104: ファイル 'libcmt.lib'
を開くことができません。 clang-cl.exe: error: linker command failed with exit code
1104 (use -v to see invocation)
Run Build Command:"C:/ProgramData/Anaconda3/Library/bin/ninja.exe"
"cmTC_e55c4" [1/2] Building C object
CMakeFiles\cmTC_e55c4.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_e55c4.exe
FAILED: cmTC_e55c4.exe
cmd.exe /C "cd . && C:\ProgramData\Anaconda3\Library\bin\cmake.exe -E
vs_link_exe --intdir=CMakeFiles\cmTC_e55c4.dir --manifests --
C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe
/nologo CMakeFiles\cmTC_e55c4.dir\testCCompiler.c.obj
/out:cmTC_e55c4.exe /implib:cmTC_e55c4.lib /pdb:cmTC_e55c4.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 ."
RC Pass 1: command "rc /foCMakeFiles\cmTC_e55c4.dir/manifest.res
CMakeFiles\cmTC_e55c4.dir/manifest.rc" failed (exit code 0) with the
following output:
隰悶・・ョ螢ケ・・ケァ蠕娯螺郢晁シ斐<郢ァ・、郢晢スォ邵コ迹夲スヲ荵昶命邵コ荵晢ス顔クコ・セ邵コ蟶呻ス鍋クイ繝サ ninja: build
stopped: subcommand failed.
Random but also,I don't know why it's outputting Chinese characters in the end...(my system language is set to Japanese and not Chinese)

Sorry for late answer - you should also add -DCMAKE_RC_COMPILER=llvm-rc (don't remember correct name)

Related

Why is this? "error: clang: error: linker command failed with exit code 1 (use -v to see invocation)"

I was trying to build this Then i get this error
$make
g++ -o rd_view -g -Wall libcs631.a rd_direct.o pnm_display.o -lm -lX11
ld: library not found for -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [rd_view] Error 1
I'm on m1 mac ventura. Any help will be appreciated. I tried installing libx11,Xquartz also.
You do not have libX11.a in your default library directories.
Locate (or/and install)it.
To install (ubuntu):
sudo apt-get install libx11-dev
To search
Use apt-file search [filename]
Location on my computer:
/usr/lib/x86_64-linux-gnu/libX11.a
As an additional remark: move your libcs631.a after all the object files as libraries are are searched only one time so all needed symbols have to be known to the linker before it searches the library.

cmake and dll/lib link on Windows

I am working on windows with mingw and CMake.
Someone provide a library with lib, dll and header files. I am able to use it quite easily in Visual Studio projects. But I was not able to integrate it into a CMake project. There are already a lot of questions about this topic but only working for Linux, or with library build with CMake, or library where sources are available, or the same use case as me but with a lot of erratum in comments saying that the answer is never completely correct...
I would like to use the foo_function from the foo library in my bar project. I don't have the source of this foo project. Only a dll, lib and header file.
BarProject
├───
├─── CMakeLists.txt
├─── Foo
├─── include
└─── foo.h
├─── x64
|─── foo.dll
└─── foo.lib
└─── main_bar.c
This is the content of my CmakeLists.txt file :
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(bar VERSION 1.0)
# add the executable
add_executable(bar main_bar.c)
target_include_directories(bar PUBLIC "${PROJECT_BINARY_DIR}" "foo/include")
add_library(foo SHARED IMPORTED)
set_property(TARGET foo PROPERTY
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/Foo/x64/foo.dll")
set_property(TARGET foo PROPERTY
IMPORTED_IMPLIB "${CMAKE_SOURCE_DIR}/Foo/x64/foo.lib")
target_link_libraries(bar PRIVATE foo)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_SUPPRESS_REGENERATION true)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT bar)
Finally to test my work :
$ ls
main_bar.c CMakeLists.txt Foo
$ mkdir tutu
$ cd tutu
$ cmake .. && cmake --build . -v
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.29.30145.0
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
(...)
cl /c /IC:\Users\lambe\work\repository\bar\tutu /IC:\Users\lambe\work\repository\bar\. /IC:\Users\lambe\work\repository\bar\Foo\include/Wall /WX /diagnostics:column /Od /D _MBCS /D WIN32 /D _WINDOWS /D STAND_ALONE /D "CMAKE_INTDIR=\"Debug\"" /Gm- /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"bar.dir\Debug\\" /Fd"bar.dir\Debug\vc142.pdb" /external:W4 /Gd /TC /wd4996 /wd4820 /wd5045 /wd4668 /errorReport:queue C:\Users\lambe\work\repository\bar\main_bar.c
main_bar.c
Generating Code...
Link:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\lambe\work\repository\bar\tutu\Debug\bar.exe" /INCREMENTAL /ILK:"bar.dir\Debug\bar.ilk" /NOLOGO ..\Foo\x64\foo.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Users/lambe/work/repository/bar/tutu/Debug/bar.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Users/lambe/work/repository/bar/tutu/Debug/bar.lib" /MACHINE:X64 /machine:x64 bar.dir\Debug\main_bar.obj
bar.dir\Debug\main_bar.obj
main_bar.obj : error LNK2019: unresolved external symbol foo_function referenced in function main [C:\Users\lambe\work\repository\bar\tutu\bar.vcxproj]
C:\Users\lambe\work\repository\bar\tutu\Debug\bar.exe : fatal error LNK1120: 1 unresolved externals [C:\Users\lambe\work\repository\bar\tutu\bar.vcxproj]
The compilation step is working great, the issue is during the linking step.
EDIT:
Ok after the comment from Ben Voigt, I choose to explore the solution to use the gcc/g++ compiler from mingw instead of using the CL/Visual Studio thing.
The new test procedure :
$ cmake .. -G "MinGW Makefiles" && cmake --build . -v
(...)
C:\MinGW\bin\gcc.exe -DSTAND_ALONE #CMakeFiles/bar.dir/includes_C.rsp -MD -MT CMakeFiles/bar.dir/main_bar.c.obj -MF
CMakeFiles\bar.dir\main_bar.c.obj.d -o CMakeFiles\bar.dir\main_bar.c.obj -c C:\Users\lambe\work\repository\bar\main_bar.c
[100%] Linking C executable bar.exe
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\bar.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\bar.dir/objects.a
C:\MinGW\bin\ar.exe qc CMakeFiles\bar.dir/objects.a #CMakeFiles\bar.dir\objects1.rsp
C:\MinGW\bin\gcc.exe -Wl,--whole-archive CMakeFiles\bar.dir/objects.a -Wl,--no-whole-archive -o bar.exe -Wl,--out-implib,libbar.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\bar.dir\linklibs.rsp
CMakeFiles\bar.dir/objects.a(main_bar.c.obj):main_bar.c:(.text+0x4f4e): undefined reference to `foo_function'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\bar.dir\build.make:830: recipe for target 'bar.exe' failed
mingw32-make.exe[2]: *** [bar.exe] Error 1
mingw32-make.exe[2]: Leaving directory 'C:/Users/lambe/work/repository/bar/test'
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/bar.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/bar.dir/all] Error 2
mingw32-make.exe[1]: Leaving directory 'C:/Users/lambe/work/repository/bar/test'
Makefile:89: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
Ok the result is the same but at least, the command line and tools are more familiar for me.
I cannot find any reference to the foo library in the command line. Then I check the file linklibs.rsp and here we are :
$ cat CMakeFiles/bar.dir/linklibs.rsp
-lm *Foo/x64/foo.lib* -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
Is it the expected result?

Raspberry Pi Pico - setting up for C on Windows, but I can't build my own projects

I'm on Windows 10, and I installed the toolchain by following chapter 8.2 of the getting-started-with-pico.pdf file. I created the test-project by following chapter 7 of the same PDF file.
Contents of my project folder
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)
project(test_project)
pico_sdk_init()
add_executable(test test.c)
pico_enable_stdio_usb(test 1)
pico_enable_stdio_uart(test 1)
pico_sdk_import.cmake
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading PICO SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
endif ()
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)
include(${PICO_SDK_INIT_CMAKE_FILE})
File test.c
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"
const uint LED_PIN=25;
int main() {
bi_decl(bi_program_description("This is a test binary."));
bi_decl(bi_1pin_with_name(LED_PIN,"On-board LED"));
stdio_init_all();
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while(1) {
gpio_put(LED_PIN, 0);
sleep_ms(250);
gpio_put(LED_PIN,1);
puts("Hello World\n");
sleep_ms(1000);
}
}
Error
So when I run these same commands in my pico-examples folder it works completely fine.
C:\Users\bensl\Desktop\Electronics\Pico\test_project>mkdir build
C:\Users\bensl\Desktop\Electronics\Pico\test_project>cd build
C:\Users\bensl\Desktop\Electronics\Pico\test_project\build>cmake -G "NMake Makefiles" ..
Pico SDK is located at C:/Users/bensl/Desktop/Electronics/Pico/pico-sdk
PICO platform is rp2040.
PICO compiler is
PICO_GCC_TRIPLE defaulted to arm-none-eabi
PICO target board is pico.
Using board configuration from C:/Users/bensl/Desktop/Electronics/Pico/pico-sdk/src/boards/include/boards/pico.h
TinyUSB available at C:/Users/bensl/Desktop/Electronics/Pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; adding USB support.
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
ELF2UF2 will need to be built
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/bensl/Desktop/Electronics/Pico/test_project/build
C:\Users\bensl\Desktop\Electronics\Pico\test_project\build>nmake
Microsoft (R) Program Maintenance Utility Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
[ 1%] Performing configure step for 'ELF2UF2Build'
-- The C compiler identification is MSVC 19.28.29336.0
-- The CXX compiler identification is MSVC 19.28.29336.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/bensl/Desktop/Electronics/Pico/test_project/build/elf2uf2/CMakeFiles/CMakeTmp
Run Build Command(s):nmake /nologo cmTC_bce2f\fast && "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64\nmake.exe" -f CMakeFiles\cmTC_bce2f.dir\build.make /nologo -L CMakeFiles\cmTC_bce2f.dir\build
Building C object CMakeFiles/cmTC_bce2f.dir/testCCompiler.c.obj
C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1428~1.293\bin\Hostx64\x64\cl.exe #C:\Users\bensl\AppData\Local\Temp\nm3FF1.tmp
testCCompiler.c
Linking C executable cmTC_bce2f.exe
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_bce2f.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1428~1.293\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\cmTC_bce2f.dir\objects1.rsp #C:\Users\bensl\AppData\Local\Temp\nm407E.tmp
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\MSVC\1428~1.293\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\cmTC_bce2f.dir\objects1.rsp /out:cmTC_bce2f.exe /implib:cmTC_bce2f.lib /pdb:C:\Users\bensl\Desktop\Electronics\Pico\test_project\build\elf2uf2\CMakeFiles\CMakeTmp\cmTC_bce2f.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_bce2f.dir/intermediate.manifest CMakeFiles\cmTC_bce2f.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64\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:/Users/bensl/Desktop/Electronics/Pico/test_project/build/elf2uf2/CMakeFiles/CMakeOutput.log".
See also "C:/Users/bensl/Desktop/Electronics/Pico/test_project/build/elf2uf2/CMakeFiles/CMakeError.log".
NMAKE : fatal error U1077: 'echo' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64\nmake.exe"' : return code '0x2'
Stop.
I had the same problem. Then, I used cmake -G "MinGW Makefiles" .. instead. It worked for me. But for build I used nmake.
Note: MinGW GCC must be installed in your device
You should have two compilers. One cross compiler for the target and another one for the Windows host. The Windows compiler is needed to compile for example elf2uf2.exe.
The problem is here:
-- The C compiler identification is MSVC 19.28.29336.0
The compiler should be arm_none_eabi_gcc. I have the same problem, but I don't know how to change this yet.

Run executable right after building (c, cmake, make)

I would like to run my executable right after building it by using a post-build step (add_custom_command).
So far we used a WSL environment on Windows 10, which worked. Now we switch to directly building it in the Windows cmd, which gives us an error...
CMakeLists.txt extract:
# Execute unit tests after build and write output.txt into artifact directory
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${PROJECT_NAME} ARGS -v > ${ARTIFACTS}/output.txt
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "run ${PROJECT_NAME} in ${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
)
Within WSL using commands 'cmake -G "Unix Makefiles" $unit_tests_root' & 'make', CMake generates file: '...\build\Pipeline\CMakeFiles\unit_tests.dir\build.make', which shows:
...
unit_tests: CMakeFiles/unit_tests.dir/link.txt
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/mnt/c/repos/project-source/tests/unit_tests/build/Pipeline/CMakeFiles --progress-num=$(CMAKE_PROGRESS_30) "Linking C executable unit_tests"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/unit_tests.dir/link.txt --verbose=$(VERBOSE)
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "run unit_tests in /mnt/c/repos/project-source/tests/unit_tests"
cd /mnt/c/repos/project-source/tests/unit_tests && /mnt/c/repos/project-source/tests/unit_tests/build/Pipeline/unit_tests -v > /mnt/c/repos/project-source/tests/unit_tests/artifacts/output.txt
# Rule to build all files generated by this target.
CMakeFiles/unit_tests.dir/build: unit_tests
...
This toolchain is working: The unit tests are executed and output is printed to output.txt.
Within cmd using commands 'cmake -G "MinGW Makefiles" %unit_tests_root%' & 'make', CMake generates the same file: '...\build\Pipeline\CMakeFiles\unit_tests.dir\build.make', which shows:
...
unit_tests.exe: CMakeFiles/unit_tests.dir/link.txt
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=C:\repos\project-source\tests\unit_tests\build\Pipeline\CMakeFiles --progress-num=$(CMAKE_PROGRESS_30) "Linking C executable unit_tests.exe"
$(CMAKE_COMMAND) -E cmake_link_script CMakeFiles\unit_tests.dir\link.txt --verbose=$(VERBOSE)
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "run unit_tests in C:/repos/project-source/tests/unit_tests"
cd /d C:\repos\project-source\tests\unit_tests && C:\repos\project-source\tests\unit_tests\build\Pipeline\unit_tests.exe -v > C:/repos/project-source/tests/unit_tests/artifacts/output.txt
# Rule to build all files generated by this target.
CMakeFiles/unit_tests.dir/build: unit_tests.exe
...
Error message in console: (line 555 corresponds to 'cd /d C:\repos\project-source\tests\unit_tests && ...'
...
[100%] Linking C executable unit_tests.exe
run unit_tests in C:/repos/project-source/tests/unit_tests
make[2]: *** [CMakeFiles\unit_tests.dir\build.make:555: unit_tests.exe] Error 13
make[2]: *** Deleting file 'unit_tests.exe'
make[1]: *** [CMakeFiles\Makefile2:94: CMakeFiles/unit_tests.dir/all] Error 2
make: *** [Makefile:102: all] Error 2
...
What's the problem with line 555?
When I comment out the 'add_custom_command(...)' within CMakeLists.txt, the unit_tests.exe builds successfully and I can execute it manually with:
C:\repos\project-source\tests\unit_tests>.\build\Pipeline\unit_tests.exe -v > .\artifacts\output.txt
Somehow the output is not the same as the output I get when build and executed within WSL, but at least it can be executed...
Edit 1, Additional Information: I am calling these commands (cmake, make) within a python script via subprocess.run(): I have the assumption, that this locks the files - still evaluating this...
Edit 2 / Solution, I found the problem: The error actually is returned by the unit_tests.exe itself as it has 13 errors (I missed the fact, that unity exits with the amount for errors). That's why make exits with errors and deletes the executable. Now I prefer calling the executable explicitly in my python script, where I also call the cmake and make commands. #Tsyvarev, sorry for the inconvenience and thanks for your contribution. ;-)

How to compile+link a simple hello_world.c against a "cdylib" Rust lib on Windows using MSVC [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I created a dynamic lib using Rust (crate type = "cdylib"). Rust/Cargo produced two files: text_loading_lib.dll and text_loading_lib.dll.lib. I want to build a really simple project (hello_world.c) on Windows that uses a function from this lib using only MSVC (the Microsoft Visual C++ toolset) and JetBrains CLion/CMake.
main.c
#include <stdio.h>
typedef unsigned long long usize_t; // 64 bit / usize on 64 bit machine
extern void show_loading_animation_ffi(usize_t, usize_t, int, usize_t (*prog_fn)());
// function that tells the lib how many percent progress we made so far
usize_t progress_reporter() { return (usize_t) 20 }
int main(void) {
show_loading_animation_ffi(0, 100, TARGET_STDERR, progress_reporter);
return 0;
}
I'm familiar with this process on UNIX, but I have no clue how it is done on Windows. On UNIX, I'd link the shared object against main.c during compilation and provide its location using LD_LIBRARY_PATH at runtime. Is is similar on Windows?
I also tried it with a CMAKE project using JetBrains CLion, but still no success. When I try to run main() in main.c from CLion, there are always errors that the target can't be created. The library files created with Rust (text_loading_lib.dll and text_loading_lib.dll.lib) are in the same directory as CMakeLists.txt and main.c.
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(main C)
set(CMAKE_C_STANDARD 11)
add_executable(main main.c)
# the path is correct
target_link_libraries(main text_loading_animation)
#also tried: target_link_libraries(main text_loading_animation.dll)
#also tried: target_link_libraries(main text_loading_animation.dll.lib)
CLion-Output is:
[ 50%] Linking C executable main.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1426~1.288\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\main.dir\objects1.rsp /out:main.exe /implib:main.lib /pdb:C:\dev\lib-text-loading-animation-rust\calling-from-c-examples\windows\cmake-build-debug\main.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console text_loading_animation.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\main.dir/intermediate.manifest CMakeFiles\main.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: File "text_loading_animation.lib" can't be opened.
NMAKE : fatal error U1077: ""C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe"": Return-Code "0xffffffff"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
I got it working with CMake as well as the Command Line.
Cmake: CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(main C)
set(CMAKE_C_STANDARD 11)
add_executable(main main.c)
# "*.dll.lib" - important! not just *.dll
target_link_libraries(main ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/text_loading_animation.dll.lib)
# copy DLL file into target dir
add_custom_command(TARGET main POST_BUILD # Adds a post-build event to "main"
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/text_loading_animation.dll" # <--this is in-file
$<TARGET_FILE_DIR:main>) # <--this is out-file path
Now you can "run main" in Jetbrains CLion for example.
Command Line (windows batch script)
Important part is:
cl main.c /link text_loading_animation.dll.lib
main.exe
Full Batch script:
#echo off
rem: clean old data
del main.exe main.obj *.dll *.dll.lib
rem: copy rust lib into current dir
xcopy /y ..\..\target\release\text_loading_animation.dll .
xcopy /y ..\..\target\release\text_loading_animation.dll.lib .
rem: sets up Visual Studio Compilertoolsuite environment
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
rem: *.dll.lib contains header information during compile; *.dll is the runtime file that must be in the same directory/path
rem: "cl" is windows compiler: produces main.obj and main.exe; main.obj is not needed in the end;
rem: it is only for the stage between compiling and linking
rem: /W4 is highest warnings flag
cl /W4 main.c /link text_loading_animation.dll.lib
del main.obj
rem: now execute
main.exe

Resources