0

I'm having a weird problem (at least in my eyes) with C++. I've created a thread pool class, and in the main function I just pushes one element to the task queue. This gives me an error saying "undefined symbols". When the specific line is commented out, the program compiles.

Here is the push method in question:

template<class F, class ...Args>
void concurrency::thread_pool::push(F&& f, Args&&... args) {
    if (stop) {
        throw std::runtime_error("push on stopped thread_pool");
    }

    std::function<void()> func = std::bind(f, args...);

    lockGuard l(mutex);

    bool should_wake = queue.empty();

    queue.push(func);

    if (should_wake) {
        cond.notify_one();
    }
}

And here's the main function:

void hello_world() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    pool = new concurrency::thread_pool(5);
    pool->push(hello_world); // <-- Compile error!
}

This is the command:

david$ g++ -g -Wall -std=c++0x -I ./ *.cpp
Undefined symbols for architecture x86_64:
  "void concurrency::thread_pool::push<void (&)()>(void (&)())", referenced from:
      _main in ccuKcG16.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

And my g++ version:

davids-mbp:cpp-thread-pool david$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc47/4.7.3/libexec/gcc/x86_64-apple-darwin13.0.2/4.7.3/lto-wrapper
Target: x86_64-apple-darwin13.0.2
Configured with: ../configure --build=x86_64-apple-darwin13.0.2 --prefix=/usr/local/Cellar/gcc47 /4.7.3 --enable-languages=c,c++,objc,obj-c++ --program-suffix=-4.7 --with-gmp=/usr/local/opt/gmp4 --with-mpfr=/usr/local/opt/mpfr2 --with-mpc=/usr/local/opt/libmpc08 --with-ppl=/usr/local/opt/ppl011 --with-cloog=/usr/local/opt/cloog-ppl015 --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --enable-plugin --disable-nls --disable-multilib
Thread model: posix
gcc version 4.7.3 (GCC) 

Update

DavidS
  • 1,660
  • 1
  • 12
  • 26
  • Did you try with std=c++11 ? – wesley.mesquita Jan 23 '14 at 15:38
  • With OS X Mavericks this is a known issue. See http://stackoverflow.com/q/16352833/ – usr1234567 Jan 23 '14 at 15:46
  • Maybe it's worthwhile upgrading to gcc-4.8.2 (release) with homebrew / macports? – Brett Hale Jan 23 '14 at 17:03
  • Where is defined `void concurrency::thread_pool::push(F&& f, Args&&... args)` ? in a .cpp (see [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) ) ? or in .h, .inl ? – Jarod42 Jan 23 '14 at 17:09
  • It's defined in `thread_pool.hpp` first. Should the "template" be left out in the definition in the `.cpp`? – DavidS Jan 23 '14 at 17:12
  • The entire body of the implementation of the templated function should be in the `hpp` file. – woolstar Jan 23 '14 at 17:55

1 Answers1

3

Your problem might be reduced to

Because you use *.cpp in your command line

Update

I'm afraid your post doesn't provide enought information, but I bet your problem is

Community
  • 1
  • 1
nodakai
  • 7,773
  • 3
  • 30
  • 60
  • I've tried to list `thread_pool.cpp` before and after `main.cpp` but still no go. I can compile the program, but same errors occurs when linking. – DavidS Jan 23 '14 at 16:58
  • I've updated my original post with definition of both the header file and cpp file. – DavidS Jan 23 '14 at 17:16
  • Update! When I took the whole definition of the ´push´ function from the cpp file to the header file - it worked! Thank you so much for pointing me in the right direction. – DavidS Jan 23 '14 at 17:20