rafafan2010
rafafan2010

Reputation: 1579

CMake undefined reference to class

I'm trying to compile a project using CMake, but I'm getting undefined references to class functions that I have created. If I create a Makefile by hand, everything compiles fine. But when I use CMake, I'm getting undefined reference errors.

This is the directory structure:

. ├── build ├── CMakeLists.txt ├── info │   ├── indexer.pdf │   └── search.pdf ├── Makefile ├── sample │   ├── 1 │   │   └── b.txt │   ├── 2 │   │   └── c.txt │   ├── 3 │   │   └── d.txt │   └── a.txt ├── src │   ├── cpp │   │   ├── index.cpp │   │   └── record.cpp │   ├── h │   │   ├── index.h │   │   └── record.h │   └── main.cpp ├── tests │   └── a └── todo.txt

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

# Project Name
PROJECT(indexer CXX)

# Binary dir
# Built in CMake variables:
    # CMAKE_SOURCE_DIR: the directory where cmake was executed
    # CMAKE_BINARY_DIR: where the output will go 
    # EXECUTABLE_OUTPUT_PATH: common place to put executables if         you don't want it to be CMAKE_BINARY_DIR
    # LIBRARY_OUTPUT_PATH: common place to put libraries if you don't want it to be CMAKE_BINARY_DIR

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# Add additional compiler flags
# Built-ins:
#   - CMAKE_CXX_FLAGS_DEBUG = -g
#   - CMAKE_CXX_FLAGS_RELEASE = -O3 -NDEBUG
set(CMAKE_CXX_FLAGS "-std=c++0x -Wall")


include_directories("${PROJECT_SOURCE_DIR}/src/h")

# Aggregate the sources
file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/cpp")

add_executable(indexer "${PROJECT_SOURCE_DIR}/src/main.cpp" ${SOURCES})``

Error:

CMakeFiles/indexer.dir/src/main.cpp.o: In function `parse(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Index*)':
main.cpp:(.text+0x1b5): undefined reference to `Record::Record(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
main.cpp:(.text+0x1ee): undefined reference to `Index::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Record)'
CMakeFiles/indexer.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0x77d): undefined reference to `Index::Index()'
main.cpp:(.text+0x84b): undefined reference to `Index::print()'
collect2: error: ld returned 1 exit status
CMakeFiles/indexer.dir/build.make:94: recipe for target 'indexer' failed
make[2]: *** [indexer] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/indexer.dir/all' failed
make[1]: *** [CMakeFiles/indexer.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Can someone tell me what I'm doing wrong here?

Upvotes: 0

Views: 2943

Answers (1)

OrdoFlammae
OrdoFlammae

Reputation: 731

Your issue is with the line

file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/cpp")

This will set the SOURCES variable to a file named cpp in the src directory. I think you meant something like

file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/src/cpp/*.cpp")

This will catch all files that end with the extension .cpp, and put them into the SOURCES variable.

Note: I don't think you will need to pass "${PROJECT_SOURCE_DIR}/src/main.cpp" as an argument to create_executable, since your SOURCES variable should already contain the main.cpp.

Upvotes: 3

Related Questions