0

I'm working on Ubuntu 20.04.1 with gcc 9.4.0 installed. I have installed both libtbb2 and libtbb-dev via apt. Yet I simply can't compile this little example :

#include <execution>
#include <algorithm>
#include <vector>

int main(){
    std::vector<int> in = {1,2,3,4,5};
    std::vector<int> out(in.size());
    std::transform(std::execution::par, in.begin(), in.end(), out.begin(), [](int i){return i+2;});
    return 0;
}

I try to compile with g++ --std=gnu++17 -ltbb test.cpp but this gives me a pile of errors like : undefined reference to "tbb::something::function"

WARNING : No, this is not a duplicate of this question since I DID install the libraries. This is a linkage problem, not a missing library one.

TUI lover
  • 542
  • 4
  • 16
  • 1
    @pptaszni You cannot use `std::back_inserter` with parallel `std::transform`. The output iterator should be random access. So you need to `.resize()` and pass `.begin()`. – paolo Aug 08 '22 at 13:41
  • Does this answer your question? [Why does the compiler try to instantiate the wrong STL template? (BinaryOperation instead of UnaryOperation)](https://stackoverflow.com/questions/72434731/why-does-the-compiler-try-to-instantiate-the-wrong-stl-template-binaryoperatio) – paolo Aug 08 '22 at 13:42
  • @paolo I'm affraid no. The question treat the template 2 interface from the [doc](https://en.cppreference.com/w/cpp/algorithm/transform) but I'm using the fourth one, and the error the compiler gives me seems to relate to linkage error – TUI lover Aug 08 '22 at 13:48
  • @paolo thx, then only second part of my comment is ok: "create out vector with enough elements out(in.size()), otherwise it's out of bounds access" – pptaszni Aug 08 '22 at 13:50
  • 2
    Problem is support from compiler side. With very next version it is ok: https://godbolt.org/z/edTEnrf8j See also [C++ compiler library support](https://en.cppreference.com/w/cpp/compiler_support/17#C.2B.2B17_library_features) - note star for gcc 9* it says `-ltbb` should be enough but same problem comes up. – Marek R Aug 08 '22 at 13:58
  • basically `sudo apt install libtbb-dev` - from marked duplicate. So apparently gcc 9 has external dependency. – Marek R Aug 08 '22 at 14:04
  • Thank you very much for your answer @MarekR, but I have already installed this, as mentioned in my question. It seems like even doing this is not enough for gcc9, I will have to upgrade to gcc10 – TUI lover Aug 08 '22 at 14:07
  • 1
    Got it working on [godbolt with gcc 9.4](https://godbolt.org/z/rnas6hnov). Note it fails when [newer version of tbb is selected](https://godbolt.org/z/ssdqbj9Ex). – Marek R Aug 08 '22 at 14:11
  • compiling with g++10 gave me the same error. I'm affraid the lubtbb-dev has some issues – TUI lover Aug 08 '22 at 14:18
  • @MarekR the libtbb package from ubuntu 20lts install tbb2020.1-2, which seems not enough for std17 requirements. I guess this problem will be solved with ubuntu 22lts – TUI lover Aug 08 '22 at 14:52
  • Correct duplicate added. – n. m. could be an AI Aug 12 '22 at 16:47
  • @n.1.8e9-where's-my-sharem. No it’s not. Please read more carefully the question and the comments above before doing that – TUI lover Aug 12 '22 at 21:59
  • 2
    I have read the question and all the comments several times. I don't see where the order of libraries on the command line is mentioned. If I'm missing something I apologise. Did you try to move `-ltbb` after `test.cpp`? If this doesn't work, please add the output of `gcc --verbose -Wl,--verbose test.cpp -ltbb -std=gnu++17`. No need to copy all the messages about undefined references, one or two is enough. – n. m. could be an AI Aug 13 '22 at 06:45
  • @n.1.8e9-where's-my-sharem. Sorry, I had missunderstood what you meant by wrong order. This works just fine, I will close the question – TUI lover Aug 16 '22 at 08:14

0 Answers0