2

I have this directory struct:
├── bin
├── build
├── calculator
│   ├── build
│   ├── calculator.c
│   └── CMakeLists.txt
├── CMakeLists.txt
├── compcalc
│   ├── build
│   ├── CMakeLists.txt
│   ├── compcalc.c
│   └── compcalc.h
├── include
│   └── calculator.h
├── obj
│   ├── libcompcalc.a
│   └── libsimpcalc.a
├── simpcalc
│   ├── build
│   ├── CMakeLists.txt
│   ├── simpcalc.c
│   └── simpcalc.h

simpcalc and compcalc are built fine and after running "make install" the files are put in the /obj directory. But when I try to build calculator i get the following errors:

/usr/bin/ld: cannot find -lsimpcalc
/usr/bin/ld: cannot find -lcompcalc

The CMakefile.txt is:

cmake_minimum_required(VERSION 2.8.9)
project(calculator)
set(CMAKE_BUILD_TYPE Release)

include_directories(../include)

file(GLOB SOURCES "*.c")

set ( PROJECT_LINK_LIBS libsimpcalc.a  libcompcalc.a)
link_directories({calculator_SOURCE_DIR}/../obj )

add_executable(calculator ${SOURCES})
target_link_libraries(calculator ${PROJECT_LINK_LIBS} )

Not sure why it is looking for simpcalc when in the CMakeLists.txt I specifically say libsimpcalc.a.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Tim Edwards
  • 97
  • 1
  • 2
  • 13
  • As you know full path to the library file, it is preferred to use it in `target_link_libraries()` call: `set(PROJECT_LINK_LIBS ${calculator_SOURCE_DIR}/../obj/libsimpcalc.a ${calculator_SOURCE_DIR}/../obj/libcompcalc.a)`. BTW, you forgot `$` symbol in `link_directories()` call. Hint for formatting code/message on Stack Overflow: Select code/text and use `{}` button or `Ctrl+K`. – Tsyvarev Mar 15 '17 at 19:02
  • the missing '$' solved the problem! I have been looking at that and searching the net for 2 days! THANKS – Tim Edwards Mar 15 '17 at 20:03

1 Answers1

0

Why do you want to hardcode the static library dependencies?

Assuming your dependency static libraries are built as follows:

add_library(simpcalc simpcalc.c)
target_include_directories(simpcalc PUBLIC ${CMAKE_CURRENT_LIST_DIR})

and

add_library(compcalc compcalc.c)
target_include_directories(compcalc PUBLIC ${CMAKE_CURRENT_LIST_DIR})

You can then build your executable as follows:

add_executable(calculator calculator.c)
target_include_directories(calculator PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(calculator simpcalc compcalc)

This leaves the hard work of figuring out where your libraries are and when to rebuild them to CMake.

Milan
  • 3,342
  • 3
  • 31
  • 40