Mihail Feraru
Mihail Feraru

Reputation: 1469

Link static lib to exe in cmake (on Linux)

I have the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(cruceGame)

set(INCLUDE_PATH
    src/
    src/cli
    src/gameProtocol
    src/irc
    src/libCruceGame
    src/networking)

set(cruceGame_SOURCES
    src/singlePlayerCurses/singlePlayerCurses.c
    src/main.c)

file(GLOB libCruceGame_SOURCES "src/libCruceGame/*.c")

file(GLOB libIrc_SOURCES "src/irc/*.c")

file(GLOB libNetworking_SOURCES "src/networking/*.c")

file(GLOB libGameProtocol_SOURCES "src/gameProtocol/*.c")

file(GLOB libCli_SOURCES "src/cli/*.c")
    
set(COMPILE_FLAGS "-std=gnu99 -DGAME_HELP_MANUAL")
set(LINK_FLAGS "-lncursesw -lpthread")

include_directories(${INCLUDE_PATH})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINK_FLAGS}")

add_library(CruceGame STATIC ${libCruceGame_SOURCES})
add_library(Irc STATIC ${libIrc_SOURCES})
add_library(Networking STATIC ${libNetworking_SOURCES})
add_library(GameProtocol STATIC ${libGameProtocol_SOURCES})
add_library(Cli STATIC ${libCli_SOURCES})

file(GLOB LIBS "*.a")

add_executable(cruceGame ${cruceGame_SOURCES})

The compilation of the static libs is OK, but I don't know how to link them to the final executable and I get a lot of "undefined reference" errors on executable linkage. So, how can I do this? I tried: target_link_libraries(cruceGame ${LIBS}) , but it doesn't work.

Thanks!

Upvotes: 1

Views: 147

Answers (1)

arrowd
arrowd

Reputation: 34391

As easy as target_link_libraries(cruceGame Irc Networking GameProtocol ...).

PS: And naming library and executable targets with names only different case-wise is not a good idea. I suspect, this would cause problems on Windows.

Upvotes: 1

Related Questions