-1

I have 2 static libraries A:\\A.lib, B:\\B.lib I want to link to using MSVS2019

This is my cmakelists.txt

add_executable(main ${src}/main.c)
SET(A "A:/")
SET(B "B:/")

find_library(AA "${A}/A.lib")
find_library(BB "${B}/B.lib")
target_link_libraries(main ${AA})
target_link_libraries(main ${BB})

I have got a lot of unresolved external symbols then.

  • Possible duplicate: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library – cs1349459 Nov 07 '22 at 12:19
  • I do the same but it doesn't work for me – green cross Nov 07 '22 at 12:20
  • 1
    "it doesn't work" is a terrible diagnostic. Show the actual full linker commandline and any errors. – Botje Nov 07 '22 at 12:23
  • "I have got a lot of unresolved external symbols then." - We could only *guess* what kind of the unresolved symbols you got. Please, show (add to the question post) the **exact error message** you got. If there are many error messages, then make sure to provide at least the very **first** one. See also [ask]. – Tsyvarev Nov 07 '22 at 12:24

1 Answers1

2

Don't hardcode paths to external libs.

cmake_minimum_required(VERSION 3.18)
project(foo)

add_executable(bar ...)

find_library(libA NAMES A REQUIRED)
find_library(libB NAMES B REQUIRED)
target_link_libraries(bar PRIVATE ${libA} ${libB})
cmake -S . -B build -DCMAKE_LIBRARY_PATH="<path/to/libA/folder>;<path/to/libB/folder>"

Though, it would be far more robust if these two external libraries could provide a CMake config file with imported targets.

SpacePotatoes
  • 659
  • 5
  • 11