cameracode
cameracode

Reputation: 97

Undefined symbols for architecture x86_64: "Graphics::Graphics()", referenced from: _main in main.cpp.o

I can't get my project to build in CLion after creating the class graphics.h and implementing it main.cpp, I've tried modifying my CMakeLists.txt 6 ways from Sunday. Can anyone help me and give me a second set of eyes to what I'm missing? I've tried stackoverflow and everywhere else. I installed my SDL packages with brew and created a pointer in ~bash.profile for where all my libs are

main.cpp

/* Main.cpp
 * The entry point of the program
 * */
#include "graphics.h"

int main(int argc, const char* argv[]) {
    Graphics graphics;
    while(true) {

    }

    return 0;
}

edit: graphics.cpp

#include <SDL2/SDL.h>
#include "graphics.h"

/* Graphics class
 * Holds all information dealing with graphics for the game
 */

Graphics::Graphics() {
    SDL_CreateWindowAndRenderer(640, 480, 0, &this->_window, &this->_renderer);
    SDL_SetWindowTitle(this->_window, "CaveStory");
}

Graphics::~Graphics() {
    SDL_DestroyWindow(this->_window);
}

~bash.profile

export PATH="/usr/local/sbin:$PATH"
export C_INCLUDE_PATH=/usr/include:/usr/local/include
export CPLUS_INCLUDE_PATH=/usr/include:/usr/local/include

CMakeLists.Txt -

cmake_minimum_required(VERSION 3.3)
project(cavestory_development)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${cavestory_development}/cmake")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake")

set(SOURCE_FILES source/src/main.cpp)

include_directories(${cavestory_development})
include_directories("source/headers")
include_directories("/usr/bin/clang++")

find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)

include_directories(${SDL2_INCLUDE_DIRS})
link_directories(${SDL2_LIBRARY_DIRS})

add_executable(cavestory_development ${SOURCE_FILES})

target_link_libraries(cavestory_development
       ${SDL2_LIBRARIES}
       ${SDL2_IMAGE_LIBRARIES}
       ${SDL2_TTF_LIBRARIES})

Build Output from CLion -

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build      /Users/User/Library/Caches/CLion12/cmake/generated/791c20f4/791c20f4/Debug --   target cavestory_development -- -j 4
[ 50%] Linking CXX executable cavestory_development
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -o cavestory_development -L/usr/local/lib -search_paths_first -headerpad_max_install_names CMakeFiles/cavestory_development.dir/source/src/main.cpp.o -lSDL2 -rpath /usr/local/lib -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "Graphics::Graphics()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see   invocation)
make[3]: *** [cavestory_development] Error 1
make[2]: *** [CMakeFiles/cavestory_development.dir/all] Error 2
make[1]: *** [CMakeFiles/cavestory_development.dir/rule] Error 2
make: *** [cavestory_development] Error 2

Upvotes: 0

Views: 2565

Answers (1)

cameracode
cameracode

Reputation: 97

Thanks for all the help @xaxxon, also thanks for making me find the answer as well!

I'm still testing which of these are redundant or not, but I finally got the project to build with the following modifications

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(cavestory_development)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${cavestory_development}/cmake")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake ")

include_directories(${cavestory_development})
include_directories("/usr/bin/clang++")
include_directories(source/headers)

find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)

include_directories(${SDL2_INCLUDE_DIRS})
link_directories(${SDL2_LIBRARY_DIRS})

set(SOURCE_FILES source/src/main.cpp source/src/graphics.cpp source/headers/graphics.h)
add_executable(cavestory_development ${SOURCE_FILES})

target_link_libraries(cavestory_development
        ${SDL2_LIBRARIES}
        ${SDL2_IMAGE_LIBRARIES}
        ${SDL2_TTF_LIBRARIES})

Majority of the changes took place it set(SOURCE_FILES) I didn't realize that I had to add graphics.cpp and graphics.h. I also didn't realize that my include_directories was incorrect. Thanks again everyone for all the help!

Upvotes: 1

Related Questions