Recent versions of GCC require that you put the object files and libraries in the order that they depend on each other - as a consequential rule of thumb, you have to put the library flags as the last switch for the linker.
those anyone know if nvcc linker works the same way ?
Since nvcc depends on the host compiler and linker for various operations, it’s probably a good idea to follow whatever rules are associated with your host compiler.
Ok thank you very match, let me be more specific. I asked this question because, I currently working on a large project with was originally compiled with nvidia’s nvcc. And because the code also need to work on cpu i tried to compile the project with g++ (no cuda code). However, while i was trying doing that i got a lot of “reference to function undefined” errors on function nvcc could link. so my main question is this linker error appears because there is differences between nvcc to g++ linker, or linking process?
I suspect the reason the order matters when linking with GCC and not when linking with NVCC, is that under the hood NVCC calls GCC with the flags -Wl,–start-group … -Wl,–end-group around the object files and libraries (-Wl,… is used to pass flags directly to the linker, ld).
According to the ld manual:
–start-group archives --end-group
The archives should be a list of archive files. They may be either explicit file names, or -l options.
The specified archives are searched repeatedly until no new undefined references are created. Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference. By grouping the archives, they all be searched repeatedly until all possible references are resolved.
Using this option has a significant performance cost. It is best to use it only when there are unavoidable circular references between two or more archives.