mkanakis
mkanakis

Reputation: 383

How to tell CMake to link executable against library?

My source tree is

So Demo is the executable which is done with add_executable() and Library is obviously the library. How can those 2 be linked together?

Because right now I am using target_link_libraries(Demo Library) but I am getting Error: LNK2019 which I think is caused by not linking successfully.

Any ideas?

Thanks.

edit: On root CMakeLists.txt those 2 are added as -> add_subdirectory(Library) add_subdirectory(Demo).

edit2:
Demo CMakeLists

add_executable(Demo ${Headers}
                    ${Source})

target_link_libraries(Demo ${blahblah}
                           ${Library})

Library CMakeLists

add_library(Library blahblah.cpp
                    blahblah.h
                    foo.cpp
                    foo.h)

Upvotes: 4

Views: 8146

Answers (2)

jhauris
jhauris

Reputation: 1143

In your example above, Library is a target name, not a variable. When you link it to your executable, use target_link_libraries(Demo Library) (no '${}' around 'Library').

Upvotes: 0

mascoj
mascoj

Reputation: 1329

This is incorrect:

target_link_libraries(Demo ${blahblah}
                           ${Library})

What you are saying is to use the string variable called Library, which won't exist.

Use:

target_link_libraries(Demo ${blahblah}
                               Library)

so that CMake will know you are referencing the target "Library" and not a variable.

Upvotes: 3

Related Questions