0

My project tree is:

Name
    - Catch-tests
       -- Catch CMAKE
       -- test.cpp (included as exec in cmake)
    - include (stores headers)
       -- foo.h
    - src (stores implementation)
       -- foo.cpp
    - Cmake for entire project
    - main.cpp

So when I include foo.h in test.cpp and try to call function, which is declared in foo.h and implemented in foo.cpp Catch says that everything is failed and there is no any implementation. CMAKE (catch2)

add_executable(Catch_tests_run AbstractTests.cpp ShadingTests.cpp)

CMAKE (project)

cmake_minimum_required(VERSION 3.16)
project(Hexa)

set(CMAKE_CXX_STANDARD 20)

# GLAD is OpenGL library loader.
add_library("glad" "lib/glad/src/glad.c")
include_directories("lib/glad/include")

# SDL2 provides low level controll to keyboard, sound, video, etc.
find_package(SDL2 REQUIRED)
include_directories(Hexa ${SDL2_INCLUDE_DIRS})

add_executable(Hexa main.cpp
                src/ShadingEngine.cpp
                include/ShadingEngine.h
                lib/imgui/imconfig.h
                lib/imgui/imgui.cpp
                lib/imgui/imgui.h
                lib/imgui/imgui_demo.cpp
                lib/imgui/imgui_draw.cpp
                lib/imgui/imgui_internal.h
                lib/imgui/imgui_widgets.cpp
                lib/imgui/imstb_rectpack.h
                lib/imgui/imstb_textedit.h
                lib/imgui/imstb_truetype.h
                lib/imgui/imgui_impl_opengl3.cpp
                lib/imgui/imgui_impl_opengl3.h
                lib/imgui/imgui_impl_sdl.cpp
                lib/imgui/imgui_impl_sdl.h)

target_link_libraries(Hexa "glad")
target_link_libraries(Hexa ${SDL2_LIBRARIES})

add_subdirectory(Catch_tests)
m8dotpie
  • 3
  • 2

1 Answers1

0

I am assuming you cannot find foo.h within the test.cpp.

If that is the case all you need is a target_include_directories(Catch_tests_run ${CMAKE_SOURCE_DIR}/include)

This should make foo.h available.

user3389943
  • 147
  • 1
  • 7
  • test.cpp include foo.h successfully, but it does not compile complaining that there is no implementation of function called, but the implementation is stored in src/foo.cpp – m8dotpie Aug 11 '20 at 19:32
  • Answer to your comment to the post: foo.h contains class ShadingEngine with method "empty", which is implemented in foo.cpp as ShadingEngine::"empty", but test.cpp does not compile saying: Undefined symbols for architecture x86_64: "ShadingEngine::empty – m8dotpie Aug 11 '20 at 19:35
  • You have to add this then. add_library(foo include/foo.h src/foo.cpp) and later target_link_libraries(Catch_tests_run foo) – user3389943 Aug 12 '20 at 17:33