I know that everyone either compiles everything in 2 well separated stages (sources->objects->executables) or sometimes in 1 stage (nvcc *.cu *.cpp )
but what if someone mixed the stages and wrote nvcc a.cu b.o, i.e invoked nvcc on both objects and sources?
It turns out that the result depends on the order the parameters are supplied to nvcc.
If you first feed it with *.cu, and then with *.o, nvcc will treat *.o files as SOURCE files and produce hundreds of meaningless error messages.
If you first feed it with an object file, then with sources, the compilation will proceed smoothly
Here’s an example
Assume we’ve got three files: test.h test.cu main.cu:
// test.h
#pragma once
void foo();
// test.cu
#include "test.h"
#include <iostream>
void foo()
{
std::cout << "foo!\n";
}
// main.cu
#include "test.h"
int main()
{
foo();
}
Now, let’s make a test.o file
> nvcc -c test.cu -o test.o
And compile the program “objects-first” way i.e.
> nvcc test.o main.cu && ./a.out
foo!
The program compiles and runs!
Now let’s change the order of the arguments to nvcc, i.e. let’s compile the program “sources first” way:
> nvcc main.cu test.o >& error.log
The error.log file contains 624 lines with warnings and errors and looks like this:
test.o:1:8: warning: null character(s) ignored
test.o:1:18: warning: null character(s) ignored
[...]
test.o:76:129: warning: null character(s) ignored
test.o(1): error: unrecognized token
test.o(1): error: expected a declaration
[...]
Error limit reached.
100 errors detected in the compilation of "/tmp/tmpxft_00002ec6_00000000-17_test.cpp1.ii".
Compilation terminated.
As far as I know, this is an nvcc regression:
nvcc 3.2 did not have this problem
(this bug was noticed by my friend when he switched to cuda 4.0 and his project stopped compiling).
Certainly it is an nvcc error to try and compile object files as source files.