I've learned that I need to compile my main project as a library, so my unit tests can link to them. This works on Linux, but on Windows, via Visual Studio, it provides unresolved externals.
Here's the parent project CMakeLists:
cmake_minimum_required(VERSION 3.9.1)
set(CMAKE_LEGACY_CYGWIN_WIN32 1)
set (CMAKE_CXX_STANDARD 11) # Set C++11
project(CHIP8)
# To maintain a clean tree, set some useful variables so we're not building everything in root.
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # static library
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # dynamic library
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # executables
# set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules" ${CMAKE_MODULE_PATH})
# Include SFML
add_subdirectory(${CMAKE_SOURCE_DIR}/dep/SFML)
include_directories(${CMAKE_SOURCE_DIR}/dep/SFML/include)
# Add our test directory
add_subdirectory (test)
#Include where to find headers
include_directories(./src)
include_directories(./src/headers)
set(EXECUTABLE_NAME "CHIP8")
add_library (ch8lib ${CMAKE_SOURCE_DIR}/src/chipeight.cpp ${CMAKE_SOURCE_DIR}/src/headers/chipeight.h)
# Here we will include all our source files to be built to the executable (include all so they show in IDE).
add_executable(${EXECUTABLE_NAME}
${CMAKE_SOURCE_DIR}/src/main.cpp
${CMAKE_SOURCE_DIR}/src/chipeight.cpp
${CMAKE_SOURCE_DIR}/src/headers/chipeight.h
)
target_link_libraries(ch8lib sfml-graphics sfml-window sfml-audio)
target_link_libraries(${EXECUTABLE_NAME} ch8lib)
The important part is here:
add_library (ch8lib ${CMAKE_SOURCE_DIR}/src/chipeight.cpp ${CMAKE_SOURCE_DIR}/src/headers/chipeight.h)
And the unit test CMakeLists:
cmake_minimum_required(VERSION 3.9.1)
set (CMAKE_CXX_STANDARD 11) # Set C++11
set(CMAKE_LEGACY_CYGWIN_WIN32 1)
PROJECT(CHIP8TESTS)
# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
#Include where to find headers
include_directories(../src)
include_directories(../src/headers)
set(TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/tests.cpp
)
add_executable(tests ${TEST_SOURCES})
target_link_libraries(tests Catch ch8lib sfml-graphics sfml-window sfml-audio)
# enable_testing()
# add_test(NAME RunTests COMMAND tests)
I'm making sure to link SFML because it seems the errors originate from there: https://i.stack.imgur.com/dhYaG.png
Does anyone know a solution to this problem of unresolved externals?
What I want to do is this:
1) Compile my main project
2) Compile my test project
3) Run my test project
Thanks.