0

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.

Kevin
  • 979
  • 4
  • 10
  • 22
  • Am I correctly understand, that errors are about building the library `ch8lib`? It would be expected, as you don't link your library with SFML ones (you link only the executable). – Tsyvarev Apr 18 '18 at 12:02
  • You should copy the errors as text using the Output Tab instead of the Errors Tab. – drescherjm Apr 18 '18 at 12:24
  • @Tsyvarev can you make an answer with how to do that? – Kevin Apr 18 '18 at 20:58
  • I meant, that as your library `ch8lib` uses SFML functions, you should link that library with SFML one: `target_link_libraries(ch8lib sfml-graphics sfml-window sfml-audio)`. As for executable, it can be linked with your library only: `target_link_libraries(${EXECUTABLE_NAME} ch8lib)`. – Tsyvarev Apr 18 '18 at 22:03
  • @Tsyvarev Thank you, now I have the following errors: `LNK1104 cannot open file 'Debug\ch8lib.lib'` and same with `..\Debug\ch8lib.lib`. I tried commenting out the /lib lines in the first CMakeLists above, to the same error. I will update the OP with my new CMakeLists. Any idea? – Kevin Apr 18 '18 at 22:49
  • @Tsyvarev Making it static works. Do you want to add an answer so I can accept it? – Kevin Apr 19 '18 at 00:08
  • I am not sure which part of the question deserves an answer. You got unresolved symbols because you simply didn't link with a library. The error about `Debug/ch8lib.lib` being not existed is described in [that question](https://stackoverflow.com/questions/40739061/error-lnk1104-cannot-open-file-debug-myprojectlib-lib). Also, the error messages currently are not part of the question post, so after linked image will disappear, the question will have a little meaning. – Tsyvarev Apr 19 '18 at 08:07

0 Answers0