jeudi 31 octobre 2019

Why is there an undefined reference to error when compiling a Catch2 test? How should I fix it?

I'm trying to write a test script for Catch2 but am having some major headscratching errors.

I've looked online for similar cases to mine, but it seems like they also used CMakeList.txt files whereas I only downloaded the catch.hpp from the Catch2 README.

In my folder, I have getDifferences.cpp, getDifferences.hpp, and test.cpp where where I put in

#define CATCH_CONFIG_MAIN, #include "catch.hpp", #include "getDifferences.hpp" on top of its header, and have no other main() defined.

My laptop: OS: Dualboot ChromeOS / XFCE4 Compiler+version g++ (Ubuntu 5.4.0) Catch version: 2.10.0 latest version

Next, I tried compiling test.cpp with:

g++ -std=gnu++11 test.cpp

and it results in the error below:

/tmp/ccd1AxMb.o: In function `____C_A_T_C_H____T_E_S_T____0()':
test.cpp:(.text+0x27be4): undefined reference to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x27daf): undefined reference to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x27f71): undefined reference to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccd1AxMb.o: In function `____C_A_T_C_H____T_E_S_T____2()':
test.cpp:(.text+0x284b7): undefined reference to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
test.cpp:(.text+0x28740): undefined reference to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccd1AxMb.o:test.cpp:(.text+0x28ca8): more undefined references to `getDifferences(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' follow
collect2: error: ld returned 1 exit status

I'm not sure why I receive an undefined reference to getDifferences, a function that I've defined and included in the header files. Any pointers would be so helpful. Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire