1

I cannot link external library to my project

I have the following CMakeLists file

add_executable(MyProg main.cpp)

LINK_DIRECTORIES(winlib)
message(STATUS "SND FILE ${libsndfile}")
# Link
target_link_libraries(
        MyProg
        libsndfile)

I have downloaded libsndfile library, however cmake was not able to resolve it using the find_library function.

So I copied and renamed libsndfile-1.lib to my source directory

And now my project structure looks as follows

--
----winlib
------libsndfile.lib
----main.cpp
----otherfiles

When I try to build the project I get the following error

Error   LNK1104 cannot open file 'libsndfile.lib'   C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CMakeLists.txt   C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\LINK 1   

What is wrong with my project, how can I link this library ?

drescherjm
  • 10,365
  • 5
  • 44
  • 64

1 Answers1

1

find_library takes a PATHS argument with which you can specify where to look for the library.

There is a special variable CMAKE_CURRENT_SOURCE_DIR which resolves to the current directory, which you can use to specify the local subdirectory winlib

Example:

add_executable(MyProg main.cpp)

find_library(
        LIB_SND_FILE 
        libsndfile
        PATHS 
            ${CMAKE_CURRENT_SOURCE_DIR}/winlib)

target_link_libraries(
        MyProg
        ${LIB_SND_FILE})
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213