How do you add paths to libraries for cmake when using the ninja generator and the LLVM compilers?

480 views Asked by At

My main goal is to use cmake with the ninja generator to build a C++ project with the LLVM clang compiler.

I am trying to get two libraries linked so when cmake tests the compilers, the compilers will be able to find the libraries.

I am consistently getting

lld-link: error: could not open 'oldnames.lib': no such file or directory
lld-link: error: could not open 'msvcrtd.lib': no such file or directory

when these files exist.

Cmake is able to find the libraries as this runs fine

# Find the required libraries
find_library(OLDNAMES_LIB oldnames.lib)
find_library(MSVCRD_LIB msvcrtd.lib)

if (NOT OLDNAMES_LIB)
    message(FATAL_ERROR "Failed to find oldnames.lib")
endif()

if (NOT MSVCRD_LIB)
    message(FATAL_ERROR "Failed to find msvcrtd.lib")
endif()

I have tried setting linker flags several ways and it is clear from the output of cmake that the library flags are NOT being passed to the C compiler when ninja invokes it.

I tried this

target_link_libraries(sound_sculptor_test PRIVATE \"${MSVCRD_LIB}\" \"${OLDNAMES_LIB}\")

and this

target_link_options(sound_sculptor_test PRIVATE "-L\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/lib/onecore/x64/\"")

and this

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OLDNAMES_LIB} ${MSVCRD_LIB}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OLDNAMES_LIB} ${MSVCRD_LIB}")

I have also set the CFLAGS and CXXFLAGS to "-L"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/lib/onecore/x64/"", but oldnames.lib is still not found even though it is in that folder.

CMake is 3.26.4 Make is 4.4.1 Ninja is 1.11.1 LLVM is 16.0.6

I have wasted a ridiculous amount of time trying to get this to work.

Turns out I just had to delete the cmakecache after creating the CFLAGS environmental variable.

1

There are 1 answers

0
John Glen On

Setting CFLAGS and CXXFLAGS to "-L"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/lib/onecore/x64/"" allowed the libraries to be found.