0

I am trying to compile a project using cmake. I need to use an external static library in src1.c so I utilized target_link_libraries to link it to the object file. However, I am getting some complains about missing functions in src1.c which should be in the external library.

add_library(input_output OBJECT
        src/src1.c
        src/src2.c
        src/src3.c)

find_library(EXTERNAL_LIB NAMES libexternallib.a PATHS ~/lib)

target_link_libraries(input_output PRIVATE
        "${EXTERNAL_LIB}")

So I am not sure what I should do at this point. the logic sounds right at least

UPDATE1: I also added the external library directly into the linker command

add_compile_options(-Wall -Wextra --std=c99 -L~/lib -lexternallib)
add_link_options(-L~/lib -lexternallib)

but this added the library flags before the object file for the linker command, which leads to some other problem with ordering the linker arguments

  • 1
    Please, add **exact error message** to the question post. – Tsyvarev May 18 '21 at 00:16
  • src1.c:(.text+0x3c): undefined reference to `some_function' collect2: error: ld returned 1 exit status – Mohammadreza Rostam May 18 '21 at 15:24
  • Error "Undefined reference" can be issued only when link an executable or a shared library. Creation of the OBJECT library doesn't involve linking, so it cannot issue given error. Please, provide **more detailed description** of your case. – Tsyvarev May 18 '21 at 15:32
  • Correct, the problem happens when I want to link input_output to an executable target: `add_executable(my_app app/main_app.c) target_link_libraries(my_app input_output) ` – Mohammadreza Rostam May 18 '21 at 16:46

1 Answers1

0

It's hard to say what is really the reason for your error here, but I'll give it a try.

First of all, you don't need to link manually with add_compile_options or add_link_options. The cmake command target_link_libraries is doing that for you.

What I could imagine, because of the missing functions, that you need to add a header include directory from the dependency.

e.g.:

target_include_directories(input_output PRIVATE /path/to/external_library_header)
Soeren
  • 1,725
  • 1
  • 16
  • 30