Build errors on Unix (undefined reference)

Ahoy

I am getting a ton of linking errors when adding the SDL git repo as a add_subdirectory to my makelists file. The errors are all Undefined reference to SDL functions (SDL_CreateWindow etc). However I’ve found that if I build SDL separately as a static lib libSDL2.a and link directly to it (using find_library(....)), it compiles fine with no errors. Any ideas why using add_subdirectory is failing to find the static SDL lib?
FYI I’m on Manjaro, and using GCC/++ version 11. I’ve tested with other unix distros and same issues as well.

Hello,

Would you mind to provide us the full log containing the error ?
WIth that, we’ll probably be able to help you

Partial answer: link a precise library to a project, on Unix machine, means you add -L/path the the lib + -lname and name means : no prefix and no suffix. e.g. in libsomeLib.so prefix is “lib”, suffix is “.so” so you’ll have to add : " -Lsome_path_to_the_lib -l someLib" to the command line. Add a “add-subdirectory” will probably do noting helping you at compile time.

Thanks :slight_smile:

Hi

Thanks for you reply.

Would you mind to provide us the full log containing the error ?

Sure, here is the error log:
err.log (3.9 KB)

Add a “add-subdirectory” will probably do noting helping you at compile time.

I am using the terrible cmake build system, but trying to add the SDL folder as subdirectory to my main project. I.e. here is the makelists layout:

add_subdirectory(SDL)

target_link_libraries(binary
  PUBLIC SDL
)

target_include_directories(binary
  PUBLIC SDL
)

I shouldn’t need to link using -l, since add_subdirectory does all that. But when I try and compile, it just throws those linker errors shown in the log

Apologies, I was wrong since I didn’t catch you were using cmake, and you are right, there is no need to add " -l " and so on, because this is cmake job.

Now, after reading the log, indeed the libSDL2 is not found at link time. Searching with cmake and SDL2 linking keywords, looks like a similar question was already asked there (if I’m not wrong):

and maybe:

Anyway, after some search, and If I was you, I’d try :slight_smile:


add_subdirectory(SDL)
# and right after :
include_directories(SDL/include)

# adapt to your need
add_executable(${PROJECT_NAME}
    ${SOURCES}
  )

# and at the end, the linking step
# replace SDL2-static with SDL2 if shared library
  target_link_libraries(${PROJECT_NAME}
    SDL2-static
    (other libs)
  )


Last but not least : don’t forget to delete CMakeCache.txt after every try, to avoid corrupted env.

Feel free to correct me if I was wrong.

EDIT : fixed wrong order, sorry

HTH

1 Like

Superb, many thanks for the help!

# replace SDL2-static with SDL2 if shared library

Cool, I didn’t know about that! I’ve now replaced with SDL2-static and it finally compiles with no issues!
Thank very much again @ericb :slight_smile:

1 Like