user2067687
user2067687

Reputation:

CMake issues with trying to include my source files

I am trying to use the CLion IDE on Linux and I am migrating my SFML application over. It insists I use CMake which I am currently having problems with.

cmake_minimum_required(VERSION 2.6)
project(SFML2)


include_directories("${PROJECT_BINARY_DIR} src/")
include_directories("${YOUR_DIRECTORY} src")

# Define sources and executable
set(EXECUTABLE_NAME "SFML")
add_executable(${EXECUTABLE_NAME} src/SFML.cpp)


# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules"      
${CMAKE_MODULE_PATH})

find_package(SFML 2 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})

# Install target
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)


# CPack packaging
include(InstallRequiredSystemLibraries)

However I keep getting build errors

CMakeFiles/SFML.dir/src/SFML.cpp.o: In function `main':
SFML.cpp:(.text+0xca): undefined reference to `Game::Game()'
SFML.cpp:(.text+0xd9): undefined reference to `Game::run()'
CMakeFiles/SFML.dir/src/SFML.cpp.o: In function `Game::~Game()':
SFML.cpp:(.text._ZN4GameD2Ev[_ZN4GameD5Ev]+0xa2): undefined reference to `Player::~Player()'
SFML.cpp:(.text._ZN4GameD2Ev[_ZN4GameD5Ev]+0xb4): undefined reference to `Player::~Player()'
SFML.cpp:(.text._ZN4GameD2Ev[_ZN4GameD5Ev]+0x18b): undefined reference to `Player::~Player()'
SFML.cpp:(.text._ZN4GameD2Ev[_ZN4GameD5Ev]+0x1a2): undefined reference to `Player::~Player()'
collect2: error: ld returned 1 exit status
CMakeFiles/SFML.dir/build.make:90: recipe for target 'SFML' failed
make[2]: *** [SFML] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/SFML.dir/all' failed
make[1]: *** [CMakeFiles/SFML.dir/all] Error 2
Makefile:136: recipe for target 'all' failed
make: *** [all] Error 2

The Game reference is in the same location as the SFML.cpp, which is where everything is launched from.

This currently works without issue in Eclipse CDT

Upvotes: 0

Views: 1351

Answers (2)

Mark Lakata
Mark Lakata

Reputation: 20838

Two suggestions

I'm not sure what are you intending to do with the include paths. My guess is that this is a left over from some experimentation. You've told CMake to look in 2 directories, both directories that have a space in them. Because you probably don't have a directory with a space in them, I think it can be deleted safely.

include_directories("${PROJECT_BINARY_DIR} src/")
include_directories("${YOUR_DIRECTORY} src")

I'm not sure where those variables are defined, but maybe you meant this

include_directories(src)

Second, CMake can use wildcards, but that is not the recommended method. You should explicitly list all source code files in the add_exectuable() call, ie.

 add_exectuable(${EXECUTABLE_NAME}
      src/SFML.cpp
      src/SFML.h
      src/other.cpp
      src/other.h
     # and so on
 )

Adding the header files is not required, but it makes it nice if you ever compile this with Visual Studio.

Upvotes: 1

user2067687
user2067687

Reputation:

Just an update for anyone else whom might stumble across this problem. I edited these few lines into my code

# Define sources and executable
set(EXECUTABLE_NAME "SFML")

file(GLOB SRC
    "src/*.h"
    "src/*.cpp"
)

add_executable(${EXECUTABLE_NAME} ${SRC})

Seems to work now, can anyone confirm if this is the right way to do it?

Upvotes: 0

Related Questions