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} )
5
  • Don't include cpp files unless you know exactly what you're doing. What is your add_executable call like? Commented Apr 4, 2018 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 Commented Apr 4, 2018 at 12:06
  • I think I figured it out Commented Apr 4, 2018 at 12:08
  • Share your answer if you solved the problem yourself, it's nicer for the future visitors Commented Apr 4, 2018 at 12:12
  • Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? Commented Apr 4, 2018 at 12:20

1 Answer 1

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.