0

I have implementation for two classes in four different files (separate .cpp/.cu and .h for each). The problem is that when I compile the code using this command:

$ nvcc -lcuda -lineinfo -o test -I src src/schd.cpp src/ctx.cu test.cpp

I get "undefined reference to" errors for each class variables like this:

/usr/bin/ld: /tmp/tmpxft_000081c8_00000000-9_schd.o: in function `FGPRS::Scheduler::destroyAll()':
schd.cpp:(.text+0x37e): undefined reference to `FGPRS::Scheduler::_poolSize'

and it happens only for class variable members, not functions. Even though I'm using CUDA compiler (nvcc), I believe this should be a C++-related problem and has nothing to do with CUDA and this would happen even in the case of native C++ compiler (using gcc command).

Amir Fakhim Babaei
  • 1,014
  • 1
  • 5
  • 13
  • 1
    Could it be that FGPRS::Scheduler::_poolSize etc. are **static** data members ? If so - have a look here: https://stackoverflow.com/questions/11300652/static-data-member-initialization. – wohlstad May 10 '22 at 04:26
  • *"I believe this should be a C++-related problem"* -- why rely on belief when you can prove? Remove everything you believe to be unrelated and see if the problem persists. (I think that means at least removing enough code from `schd.*` and `test.*` so that you can remove `src/ctx.cu` from the compilation command.) When you have so little code that removing any more will remove the error, you have a [mre] (MRE). P.S. the MRE should be in your question so that others can copy, compile, and reproduce the error message. – JaMiT May 10 '22 at 04:26
  • @wohlstad Yes, that was exactly the problem. I had no idea that for static variables I should have a definition as well. Thank you very much! – Amir Fakhim Babaei May 10 '22 at 21:59

0 Answers0