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?

4
  • 1
    You linked answer explicitely uses a list (of 1 but it could be more) object files. Commented Jan 29, 2020 at 5:51
  • You should find how GLOB_RECURSE works here. This should help :) Commented Jan 29, 2020 at 7:13
  • @Frank: I agree. But I was looking for a wildcard based approach rather than listing all the object files. Commented Jan 29, 2020 at 9: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. Commented Jan 29, 2020 at 9:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.