简体   繁体   中英

How can I solve undefined reference errors with Conan and SDL2 on Ubuntu 16.10?

I am trying to get conan working in a test project by following their Geting Started guide so that I can begin using it in a real project. I am attempting to use SDL2 with glew in a C++ project compiled with g++ on Ubuntu 16.10 x64.

My conanfile.txt looks like this:

[requires]
SDL2/2.0.5@lasote/stable
glew/2.0.0@coding3d/stable

[generators]
cmake

My CMakeLists.txt file looks like this:

project(conantest)

cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 11)

include(conan/conanbuildinfo.cmake)
conan_basic_setup()

set(SRC_FILES main.cpp Display.cpp)
add_executable(conantest ${SRC_FILES})

My project consists of a directory containing CMakeLists.txt, conanfile.txt, all of my source code, a build directory for Cmake, a conan directory for Conan, and a rebuild.sh. I am using this rebuild.sh script to clean and build the project whenever I make a change to the build environment (the rm -r's, although suboptimal once it is working are there to ensure any errors from the previous setup are removed once I attempt a fix).

My rebuild.sh looks like this:

rm -r ./build/*
rm -r ./conan/*
cd conan
conan install ..
cd ../build
cmake ..
make

When I run this script, everything seems to be working until the final executable is linked. When this happens, I get Display.cpp:(.text+0x8a): undefined reference to `SDL_Init' . To solve this, I have tried the solutions here and here . Although these are not specific to SDL, I was unable to find any resources that are.

Here is a zip file of the full environment that I am using, including a MCVE.

You need to tell CMake to link against the libraries yourself, since conanbuildinfo.cmake cannot know the name of your target.

Simply adding this line to the bottom of your CMakeLists.txt file will work:

conan_target_link_libraries(conantest)

This command will call target_link_libraries for all dependencies specified in the conanfile.

Here's another CMakeLists.txt example if you would like to use a more "modern" CMake approach which focuses more on targets instead of global settings:

cmake_minimum_required(VERSION 3.5)
project(conantest)

include(conan/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

add_executable(conantest main.cpp Display.cpp)
set_property(TARGET conantest PROPERTY CXX_STANDARD 11)
target_link_libraries(conantest CONAN_PKG::SDL2 CONAN_PKG::glew)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM