# Undefined reference problems - C++

**URL:** https://discourse.cmake.org/t/undefined-reference-problems-c/4189
**Category:** Code
**Tags:** os:linux
**Created:** [October 1, 2021, 3:36pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189 "2021-10-01T15:36:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![UltraBlack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ultrablack/32/1840_2.png) [@UltraBlack](https://discourse.cmake.org/u/UltraBlack)
#### Post date: [October 1, 2021, 3:36pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/1 "2021-10-01T15:36:25Z")

</div>

Hey there,  
I’m facing some issues with undefined references in my code and I’ve come to the conclusion, that CMake must be some kind of error source - There are no compiling problems, it just fails to … do stuff whenever I am using my thread interface  
Please don’t be too harsh with your comments - I am new to CMake and C++ (Is this even the right place to ask… - Stackoverflow is such a tracking hazard)

Utils.cpp: [0bin - encrypted pastebin](https://0bin.net/paste/7wnpa9uL#kDx6ySqJPDKqWoHm0eLhsmpAOWiNYQcXLUfIBBO8jI5)  
Utils.hpp: [0bin - encrypted pastebin](https://0bin.net/paste/Lab4owO2#r+pQ7TwZghXUJbUnegWecMOnLRdPr+SrNjf3UCSsBFP)  
CMakeLists.txt (reached link cap):

```auto
add_executable(WS WordSearch.cpp)
add_library(U Utils/utils.cpp Utils/utils.hpp)
target_link_libraries(WS U)

```

Terminal output:

```auto
/sbin/ld: libU.a(utils.cpp.o): warning: relocation against `_ZN11threadUtils13ThreadManager6pausedE' in read-only section `.text._ZZN11threadUtils13ThreadManager3runEvENKUlvE_clEv[_ZZN11threadUtils13ThreadManager3runEvENKUlvE_clEv]'
/sbin/ld: libU.a(utils.cpp.o): in function `threadUtils::ThreadManager::run()::{lambda()#1}::operator()() const':
/home/maus/Cxx/WordSearch/src/Utils/utils.cpp:106: undefined reference to `threadUtils::ThreadManager::paused'
/sbin/ld: /home/maus/Cxx/WordSearch/src/Utils/utils.cpp:106: undefined reference to `threadUtils::ThreadManager::paused'
/sbin/ld: libU.a(utils.cpp.o): in function `std::thread::thread<threadUtils::ThreadManager::run()::{lambda()#1}, , void>(threadUtils::ThreadManager::run()::{lambda()#1}&&)':
/usr/include/c++/11.1.0/bits/std_thread.h:136: undefined reference to `pthread_create'
/sbin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/WS.dir/build.make:98: src/WS] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: src/CMakeFiles/WS.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

```

What’s wrong here - Is CMake even causing this?  
Thanks!

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 1, 2021, 4:43pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/2 "2021-10-01T16:43:38Z")

</div>

The problem seems to be you’re building a STATIC library (check [https://cmake.org/cmake/help/latest/command/add\_library.html](https://cmake.org/cmake/help/latest/command/add_library.html) to know why it’s static). This library type is by default compiled without position independent code (PIC) and, if not explicitly written in CMake, does not carry transitive linking information.  
Additionally, if I remember correctly, to use C++ thread thingies on linux, you must link to a threading library by yourself (see docu for `find_package()` and `FindThreads` module).

---

<div class="post-metadata">

### Author: ![UltraBlack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ultrablack/32/1840_2.png) [@UltraBlack](https://discourse.cmake.org/u/UltraBlack)
#### Post date: [October 1, 2021, 4:52pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/3 "2021-10-01T16:52:36Z")

</div>

Thanks for the answer!

Threading seems to be pretty easy on linux, as stated [here](https://stackoverflow.com/questions/17004290/c-threading-in-linux).

I just marked my library as `SHARED` and it’s now looking better:

```auto
[75%] Linking CXX executable WS
/sbin/ld: libU.so: undefined reference to `pthread_create'
/sbin/ld: libU.so: undefined reference to `threadUtils::ThreadManager::paused'
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/WS.dir/build.make:98: src/WS] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: src/CMakeFiles/WS.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

```

I am confused why it’s having problems - It doesn’t even print out any lines.  
EDIT: Line 106-108 are causing the “paused” problem (if (std::count(paused.begin(), paused.end(), id)),  
Commenting out the whole run() method fixes all compilation errors - Huh.

EDIT 2: Should’ve read the whole comment

---

<div class="post-metadata">

### Author: ![UltraBlack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ultrablack/32/1840_2.png) [@UltraBlack](https://discourse.cmake.org/u/UltraBlack)
#### Post date: [October 1, 2021, 5:24pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/4 "2021-10-01T17:24:34Z")

</div>

Oh nice! I got it to an almost working state:

```auto
add_executable(WS WordSearch.cpp)
find_package (Threads)
add_library(U SHARED Utils/utils.cpp Utils/utils.hpp)
target_link_libraries(U pthread)
target_link_libraries(WS U)

```

Compiles perfectly when I comment out that paused searcher part - Why is that one still doing weird stuff…

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 1, 2021, 5:24pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/5 "2021-10-01T17:24:47Z")

</div>

I’ve just checked - for the threading on linux you really need to link pthreads - that will take care of the missing `pthread_create`.

I believe the second error comes from ODR violation you have in your code - you define `threadUtils::ThreadManager` class twice - once in the hpp and once in the cpp file and those two are not identical which they must be according to C++ standard or you’re in undefined behaviour teritory. In fact the same goes for `progress::ActionSpinner`.

---

<div class="post-metadata">

### Author: ![UltraBlack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ultrablack/32/1840_2.png) [@UltraBlack](https://discourse.cmake.org/u/UltraBlack)
#### Post date: [October 1, 2021, 5:32pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/6 "2021-10-01T17:32:23Z")

</div>

Something like this I guess?

 ![Screenshot_194](https://discourse.cmake.org/uploads/default/original/2X/7/71e14aab6e7f7a28336a934c140d8c943e0c8162.png)  
This might just be completely bogus at this point.

… I am still getting the same error regardless

EDIT again: Actually using my interface:

```auto
/sbin/ld: CMakeFiles/WS.dir/WordSearch.cpp.o: in function `main':
/home/maus/Cxx/WordSearch/src/WordSearch.cpp:214: undefined reference to `threadUtils::ThreadManager::kill()'
/sbin/ld: /home/maus/Cxx/WordSearch/src/WordSearch.cpp:262: undefined reference to `progress::ActionSpinner::~ActionSpinner()'
/sbin/ld: /home/maus/Cxx/WordSearch/src/WordSearch.cpp:262: undefined reference to `progress::ActionSpinner::~ActionSpinner()'
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/WS.dir/build.make:98: src/WS] Error 1
make[1]: *** [CMakeFiles/Makefile2:126: src/CMakeFiles/WS.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

```

CMake is hard

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 1, 2021, 5:57pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/7 "2021-10-01T17:57:26Z")

</div>

This is not CMake - this is really C++. And yeah - not the easiest thing on Earth 😜

In any case, I think your .hpp looks OK, except you need to declare `action()` as pure virtual already there, but I’m not sure if the way you’ve written your .cpp is legal - I’ve never seen a class redeclared like that in the .cpp. Maybe try more standard approach, like:

```auto
namespace threadUtils {
 
  std::vector<std::time_t> ThreadManager::threads;
  std::vector<std::time_t> ThreadManager::paused;
 
  void ThreadManager::kill() { threads.erase(std::find(threads.begin(), threads.end(), id)); }
 
  void ThreadManager::pause(bool cont) {
    std::vector<std::time_t>::iterator it =
        std::find(paused.begin(), paused.end(), id);
    if (it != paused.end() && !cont) {
      paused.erase(it);
    } else if (cont) {
      paused.push_back(id);
    }
  }
 
  void ThreadManager::run() {
    thread = std::thread([this]() {
      while (true) {
        if (std::count(paused.begin(), paused.end(), id)) {
          continue;
        }
        action();
      }
    });
    thread.join();
  }
 
}; // namespace threadUtils

```

And the same for you ActionSpinner.  
You generally seem to have a lot of learning to do regarding C++. Maybe you should start with something simpler than threading and libraries just to get a grasp of the language? Just a suggestion - don’t yell at me 😜

---

<div class="post-metadata">

### Author: ![UltraBlack](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ultrablack/32/1840_2.png) [@UltraBlack](https://discourse.cmake.org/u/UltraBlack)
#### Post date: [October 1, 2021, 6:06pm UTC](https://discourse.cmake.org/t/undefined-reference-problems-c/4189/8 "2021-10-01T18:06:01Z")

</div>

In the end, you are 100% right about this. I’ve yet got too learn a ton about C++.  
This threading problem was more of an experiment as I’d like to have easy control about my threads. I really hate, that there’s no way to kill them externally without voodoo magic.  
I might have to resort back to just using lambda threads the good old way.  
Thanks for your efforts of trying to help me out - At least I now know, how to use SHARED libraries in CMake.  
Have a great day!  
😃
