1

I know there are a thousand questions like this but I'm struggling even after looking at those.

My directory tree

./src/main/cpp/main.cpp
./src/main/cpp/CascadeLoader.cpp
./resources/headers/CascadeLoader.h

If in main.cpp I use

#include "CascadeLoader.cpp"

my code works but if I do a

#include "CascadeLoader.h"

I get a build error of

undefined reference to CascadeLoader::CascadeLoader()

my CMakeLists.txt I added

cmake_minimum_required(VERSION 2.8)
project( ASLInterpreter )
find_package ( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( ASLInterpreter src/main/cpp/main.cpp )
target_link_libraries( ASLInterpreter ${OpenCV_LIBS} )
Andre Ellis
  • 13
  • 1
  • 8
  • Don't [include cpp files](https://stackoverflow.com/questions/9253786/include-cpp-instead-of-header-h) unless you know exactly what you're doing. What is your add_executable call like? – TioneB Apr 04 '18 at 12:04
  • Just edited the code to add my whole cmake. I'm aware of the problems with adding cpp files, I was just ensuring it was a cmake problem and not coding – Andre Ellis Apr 04 '18 at 12:06
  • I think I figured it out – Andre Ellis Apr 04 '18 at 12:08
  • Share your answer if you solved the problem yourself, it's nicer for the future visitors – TioneB Apr 04 '18 at 12:12
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tsyvarev Apr 04 '18 at 12:20

1 Answers1

2

You don't link your CascadeLoader.cpp file, this is why you get an undefined reference error.

Try

add_executable( ASLInterpreter src/main/cpp/main.cpp /src/main/cpp/CascadeLoader.cpp)

You can also group your .cpp files together which is useful if you have many of them.

set(include /src/main/cpp/CascadeLoader.cpp
            /src/main/cpp/example.cpp)
add_executable( ASLInterpreter src/main/cpp/main.cpp ${include})
TioneB
  • 468
  • 3
  • 12