0

I have a list of object files generated by a Makefile stored under say mylib directory. I am trying to link these object files while compiling one of the sub-directories in my project (I don't want to generate an executable). Here is my CMakeLists.txt file

cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
file(GLOB SOURCES "*.cpp" ".hpp")

include_directories(mylib)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/mylib)

add_library(slib SHARED ${SOURCES})

That is, mylib directory contains .h, .cc and .o files I generated after running make on mylib. When I try to compile this, I get an Undefined symbols for architecture x86_64 error for mylib functions.

How can I link multiple precompiled object files generated by an external make? This question (how to add prebuilt object files to executable in cmake) gives a method to link a single object file. How do I do this for all the object files and generated a shared library instead of an executable?

Unni
  • 5,348
  • 6
  • 36
  • 55
  • 1
    You linked answer explicitely uses a list (of 1 but it could be more) object files. –  Jan 29 '20 at 05:51
  • You should find how GLOB_RECURSE works [here](https://cmake.org/cmake/help/v3.7/command/file.html). This should help :) – RC0993 Jan 29 '20 at 07:13
  • @Frank: I agree. But I was looking for a wildcard based approach rather than listing all the object files. – Unni Jan 29 '20 at 09:38
  • @Unni: You need somehow to **list all object files** required for your library. It could be `file(GLOB)`, but it searches the files immediately at *configuration* stage. (That is, it won't find the files created during the *build* stage). As a possible approach - `GLOB` *source files* in your Makefile project, and transform resulted paths to the paths of object files which will be created by Make. – Tsyvarev Jan 29 '20 at 09:46

1 Answers1

0

I suggest to compile library "mylib" with ExternalProject (by direct call to gcc, for example) and, then use code like this:

add_library (slib SHARED ${SOURCES})
target_link_libraries (slib "mylib")

add_dependencies may be useful in some cases.

Kevin
  • 16,549
  • 8
  • 60
  • 74