0

I hope someone can help

I have a structure as follows:

Top_dir
--> CmakeLists.txt
-->include
----> defs.h (access the functions in static library)
----> moredefs.h (access the functions in static library)
----> myClass.h (Header file of my class - includes the defs.h and moredefs.h)

-->lib
---->src
------> functions.c (autogenerated - includes the defs.h and moredefs.h)
----> libsomelib.a (given to me with a compiler to make the auto generated headers)

-->src
----> main.cpp
----> myClass.cpp

my cmakelists is causing distress - I don't know how to compile to include the libsomelib.a and the c file into a library and add to the main executable.

project(myProject)
set(MODULE_NAME ${PROJECT_NAME})
set(LIB_NAME ${MODULE_NAME})

###########  SETUP  #####################

find_package(catkin REQUIRED COMPONENTS
  roscpp
)

set(MAIN
  src/main.cpp
)


set(SOURCES
  src/myClass.cpp

)

set(HEADERS
  include/myClass.h
  include/defs.h
  include/moredefs.h
)


############ LIB ########################

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

add_library(${LIB_NAME}
  ${SOURCES} 
  ${HEADERS}
)

add_library(myAttemptLib
  lib/src/functions.c
  #include/defs.h
  #include/moredefs.h
 ) 

#add_library(myAttemptLib STATIC IMPORTED)
set_target_properties(myAttemptLib PROPERTIES IMPORTED_LOCATION lib/libsomelib.a)


target_link_libraries(${LIB_NAME}
  ${catkin_LIBRARIES}
  
)

############ EXE ########################


add_executable(${PROJECT_NAME}_node ${MAIN})

add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

target_link_libraries(${PROJECT_NAME}_node
  ${LIB_NAME}
  ${catkin_LIBRARIES}
  myAttemptLib

)

This compiles and will run. I can add and use datatypes form the headers no problem, it will compile and the code runs, but as soon as I try to utilise a function which is contained inside the libsomelib.a then the compile error undefined reference to the function and recipe for target failed.

In a make file in a small test area I can compile and make executable that runs independent.

I feel it is some way that I should be compiling the .a + .c with the main exe, but have searched a lot and not trying to reach out here. I hope someone can help.

Cheers

Steve

Steve
  • 1
  • Linking with prebuilt library is performed using `target_link_libraries` command either by using full library path or by using IMPORTED target with property `IMPORTED_LOCATION` contained full library path. See more in the [duplicate question](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) and its answers. – Tsyvarev Dec 14 '21 at 23:29

1 Answers1

0

Create a CMakeLists.txt in lib. Add there a static library target calling add library with the sources there.

add_library(lib file1.cpp file2.cpp ...)

Then in your main CMakeLists.txt call this cmake file with

add_subdirectory(lib)

add the main target with

add_executable(main main.cpp ...)

include the directories

target_include_directories(main PUBLIC libdir headers ...)

and link the library with

target_link_libraries(main PUBLIC lib)
Andreas
  • 31
  • 1
  • 3
  • Hi Andreas, thanks for rapid response. I added a cmakelists with ```add_library(libname src/functions.c libsomelib.a include/defs.h include/moredefs.h )``` and moved the includes and src into the lib folder. Then in the main ```add_subdirectory(lib)``` ```target_include_directories(${PROJECT_NAME}_node PUBLIC lib lib/include)``` ```target_link_libraries(${PROJECT_NAME}_node ${LIB_NAME} ${catkin_LIBRARIES} libname``` It compiles, but still gives the same result - undenfined reference - did I misunderstand your post? – Steve Dec 14 '21 at 23:07
  • add_library(libname PUBLIC src/functions.c include/defs.h include/moredefs.h) target_include_directories(${PROJECT_NAME}_node PUBLIC lib/include) target_link_libraries(${PROJECT_NAME}_node PUBLIC ${LIB_NAME}) You may also need in lib cmakefile: include_directories(lib) link_directories(lib) Also in target_link_libraries the order of the libraries is important. That could be a cause of undefined reference error. – Andreas Dec 15 '21 at 00:13