CUDA multiple definition error during linking [duplicate] - c++

This question already has answers here:
Multiple definition of ... linker error
(3 answers)
Closed 6 years ago.
I am starting to use CUDA and thrust for a project of mine, so I am still new to some of it aspects. I ran into the following problem.
I have to separate .cu files which I want to use with a common header file which has a struct that both of them shall be able to use. It is something like this:
////////Global.h
#ifndef global_h
#define global_h
struct globalParam
{
uint64_t spaceToUse;
globalParams() : spaceToUse(1024*1024*1024) {}
};
globalParam glParam;
#endif
The First .cu file looks like this:
////////firstcufile.cu
#ifndef firstcufile_cu
#define firstcufile_cu
#include "Global.h"
template<typename T>
QVector<T> GPUcalculateNormSq(const QVector<T>& real, const QVector<T>& imag)
{
QVector<T> result (real.size());
uint64_t maxSpace = glParam.spaceToUse;
//Some Code to use thrust and using tops maxSpace bytes.
return result;
}
template QVector<float> GPUcalculateNormSq(const QVector<float>& real, const QVector<float>& imag);
template QVector<double> GPUcalculateNormSq(const QVector<double>& real, const QVector<double>& imag);
#endif
The second .cu file looks like this:
////////secondcufile.cu
#ifndef secondcufile_cu
#define secondcufile_cu
#include "Global.h"
template<typename T>
double getMean(const T& vec)
{
uint64_t spaceNeededOnGPU = vec.size() * sizeof (T);
uint64_t maxSpace = glParam.spaceToUse;
//Some code to make sure tops maxSpace bytes on GPU
double sum = thrust::reduce(std::begin(vec), std::end(vec));
return sum / vec.size();
}
template double getMean(const QVector<float>& vec);
#endif
Now the error I get is:
secondcufilecuda_d.o:(.bss+0x18): multiple definition of `glParam'
firstcufilecuda_d.o:(.bss+0x18): first defined here
The functions above seem similar, but thats becauseI tried to make them as simple as possible. It would be possible to write everything into a single .cu file, but I would like to split it up if possible.
What am I doing wrong with the linker? I am compiling and linking from within a Qt Creator project. Let me know if you need my lines from the .pro file to know how I use the nvcc compiler.

It's due to the fact that Global.h is included multiple times and each time it is included it provides: globalParam glParam;. That is not a forward declaration (i.e. not just a type signature) but amounts to an actual instantiation of a globalParam struct. This then, causes there to be two variables both named glParam (each corresponding to a separate #include of Global.h) and that gives you multiple definition errors.
Quick fix: try using extern if you wish to share your global variable (then the linker knows that it's just a reference to, well, a 'external' symbol).
Better fix: consider refactoring your code to pass in the global parameter via reference or pointer as argument to your functions. That way you won't even have to declare the glParam variable in the header, side stepping the entire issue and making your code more easy to understand/reason about into the bargain.

Related

C++ make error: object_var has not been declared [duplicate]

This question already has answers here:
error: Class has not been declared despite header inclusion, and the code compiling fine elsewhere
(9 answers)
Closed 7 years ago.
You could have forgot to include problemclass.h from the file where you are using ProblemClass.
You could have misspelled the name of ProblemClass either in its own header file or in the place where you are using it. This can be
hard to spot if it is a capitalization error such as writing
Problemclass or problemClass instead of ProblemClass.
You could have copy-pasted your inclusion guard #defines from one header file to another and then forgot to change the defined names.
Then only the first of those two included header files would take
effect.
You could have placed ProblemClass in a namespace A, in which case you must refer to ProblemClass as A::ProblemClass if you are referring
to it from outside the namespace A.
You may be using templates and not expecting two-phase lookup to work the way it does.
You could have misspelled the file name in your include. The compiler would not report an error on that if you also have an old
version of that file under the misspelled name.
You could have made ProblemClass a macro that only gets defined after you include problemclass.h, in which case what you see as
ProblemClass gets replaced by something else by the macro
preprocessor.
You could have defined ProblemClass in a header file other than problemclass.h and then problemclass.h actually defines something
else.
The above was taken from another similar question, I found the points useful but none actually solved my problem, stated hereunder:
I'm creating a natural language processor for a robot, giving the software various objects to represent real world items in its environment (Blocks world for now), one of the objects defined in Block:
/*
* Block.h
*
* Created on: 11 Mar 2015
* Author: Edward
*/
#ifndef BLOCK_HPP_
#define BLOCK_HPP_
#include "typedefinitions.h"
#include "dynamixel.h"
#include "BasicRobotFunctions.hpp"
class Block {
public:
bool grounded;
//TODO position is a deprecated variable, replace with distance from
//pos position;
int number;
int distance;
int object_brightness;
Block(BasicRobotFunctions basic);
virtual ~Block();
void setBrightness(int brightness);
void setGrounded(bool ground);
//void setPosition(int x, int y);
void setDistance(int number);
void setNumber(int number);
//pos getPosition();
int getNumber();
int getDistance();
bool getGrounded();
int getBrightness();
int lookAround(BasicRobotFunctions basic);
};
#endif /* BLOCK_H_ */
with source file:
/*
* Block.cpp
*
* Created on: 11 Mar 2015
* Author: Edward
*/
#include "Block.hpp"
#define DEFAULT_PORTNUM 3 // COM3
#define DEFAULT_BAUDNUM 1 // 1Mbps
Block::Block(BasicRobotFunctions basic) {
grounded = false;
number = Block::lookAround(basic);
}
Block::~Block() {}
void Block::setGrounded(bool ground){
grounded = ground;
}
/*
void Block::setPosition(int x, int y){
position.x = x;
position.y = y;
}*/
void Block::setDistance(int dist){
distance = dist;
}
void Block::setNumber(int num){
number = num;
}
bool Block::getGrounded(){
return grounded;
}
/*
pos Block::getPosition(){
return position;
}*/
int Block::getNumber(){
return number;
}
int Block::getDistance(){
return distance;
}
int Block::getBrightness(){
return object_brightness;
}
//TODO Arrange function to incorporate Turn() in BasicRobotFunctions
int Block::lookAround(BasicRobotFunctions basic){
int num = 0;
dxl_initialize(DEFAULT_PORTNUM,DEFAULT_BAUDNUM);
for(int i = 0;i<360;i++){
dxl_write_word(11,32,-255);
dxl_write_word(11,30,200);
basic.Step(1);
dxl_write_word(11,32,255);
dxl_write_word(11,30,100);
if(dxl_read_byte(100,32) >= dxl_read_byte(100,52)){
num++;
}
}
dxl_terminate();
return num;
}
void Block::setBrightness(int bright){
object_brightness = bright;
}
I am however receiving the following compilation error from the constructor and from the turnAround(BasicRobotFunctions) method:
In file included from Robot.hpp:11,
from BasicRobotFunctions.hpp:12,
from main.cpp:8:
Block.hpp:23: error: expected `)' before 'basic'
Block.hpp:35: error: 'BasicRobotFunctions' has not been declared
make.exe: *** [main.o] Error 1
Having checked my other classes utilizing objects as variables I get the same error.
In response to the points in the quote:
- BasicRobotFunctions.hpp is included
- the class name is spelt the same in all different instances mentioning it
- I didn't copy paste any inclusion guard
- I didn't use any namespaces in the project
- Nor am I using any templates
- the file name isn't misspelled in my include
- I haven't defined any macros in the program
- I made sure every class was defined in its own header file
Is there any other issue my system could possibly have, any mistake I'm making or simply anything I'm doing which is bad programming practice here?
The cause of your problem:
You have a header file circular dependency problem.
main.cpp includes BasicRobotFunctions.hpp
which includes Robot.hpp
which includes Block.hpp
which includes BasicRobotFunctions.hpp.
If your header files are properly guarded against multiple inclusion (which it seems that they are), Block.hpp won't see the definitions of BasicRobotFunctions.hpp because it is already in the middle of including it.
How to spot the problem:
The source of this problem is apparent in the compilation error message and in your Block.hpp file.
The compiler is reporting an error in Block.hpp, and it is describing line by line how it got to that file via inclusions. The source to your Block.hpp file makes it clear that it is trying to include BasicRobotFunctions.hpp.
The fix:
In your case, you can modify your method signatures in Block.hpp to use a (perhaps constant) reference to the BasicRobotFunctions type, and then forward declare the type. This allows you to eliminate the dependency on the BasicRobotFunctions.hpp header file. (Block.cpp would likely need to include both Block.hpp and BasicRobotFunctions.hpp.)
//...
#include "typedefinitions.h"
#include "dynamixel.h"
class BasicRobotFunctions; // Forward declaration
//...
Block(const BasicRobotFunctions &basic);
//...
int lookAround(const BasicRobotFunctions &basic);
//...
You may be able to avoid this problem in the future by minimizing what headers are required to allow your header file to compile. This means your header file should:
Use forward declarations to types that are used.
Use references to forward declared types.
You can check that your header file has minimized its dependencies by making sure it compiles by itself. I accomplish this by including the header file first in a corresponding source file, and then make sure the source file compiles.
// Foo.cpp
#include "Foo.hpp"
//...
Well you can put a forward declaration before the class as
class BasicRobotFunctions;
class Block {
public:
bool grounded;
//TODO position is a ...
but this kind of error means that the #include "BasicRobotFunctions.hpp"
don't declare the BasicRobotFunctions. It's possible a trouble with code guards?
The circular inclusion can be solved using the forward declaration, putting correct guards in headers and moving some includes to source files.

Building fail in Eclipse C/C++ but works well in commandline

[EDIT:]
The problem seems to belong to the functions, that take default-parameters. Without separating in *.h *.cpp and main file it worked as i implemented something like:
void foo(double db;); // deklaration
void foo(double db = 4){ cout << db;} // definition
int main(){
foo(); // usage
return 1;
}
But if I separate deklaration (-> *.h), definition (-> *.cpp) and usage (-> main) compiling suddenly returns an erro telling, there is no function foo(void), as it does not recognize that there is a default parameter. Any suggestions for that?
[/EDIT]
I wrote a c++-program running somehow like:
#include <iostream>
/* other includes */
using namespace std;
class my_class
{
private:
/* variables */
public:
/* function deklarations (just some short ones are only defined not declared) */
};
ostream& operator<<(ostream &out, my_class member);
/* Definition of the member functions and of the not-member-function */
int main()
{
/*some trial codes of member-functions */
return 1;
}
In one total file all compiled well in Eclipse and worked. Now I also wanted to try seperate in a main,class-header and class-cpp file (called them "my_class.h" and my_class.cpp").
For that i put in class-header:
#ifndef MY_CLASS_H_
#define MY_CLASS_H_
#include <iostream>
/* other includes */
using namespace std;
class my_class
{
/* ... */
};
ostream & operator<<(ostream &out, my_class member);
#endif /* MY_CLASS_H_ */
I put in class-cpp:
/* Definition of the member functions and of the not-member-function */
I put in main:
#include <iostream>
#include "my_class.h"
#include "my_class.cpp"
int main()
{
/*some trial codes of member-functions */
return 1;
}
This version is compiling with the g++ command in commandline:
g++ -o main.exe main.cpp
But it does not Compile in Eclipse. There it gives me the Error:
...\my_class.cpp:11.1: error: 'my_class' does not name a type
and same for all other member functions and variables. I tried to follow the instructions from here (I put just "my_class.h" in main and my_class.cpp, but then it did not compile in Eclipse and in command line (of course then with the my_class.cpp included). Eclipse gives me an Error, that makes me believe Eclipse does not see the "my_class.cpp":
...\main.cpp:288:47: error: no matching function for call to 'my_class::foo(...)'
where foo stands for the first member-function declard in the "my_class.cpp" file. First It gave the error for the constructor too, but as I put it's definition directly into the *.h file it worked well. (That's why I think, it does not see the "my_class.cpp" file)
I think I might be missing something very trivial as I am very new to Eclipse, but I don't see it. I tried to make my questions and information as short as possible.
default-parameters need to be declared in the header-file as it contains the declarations and not in the cpp file, which contains the definitions. (An additional mistake was to declare them in the definition). Found some help here. But why did it work, as I implemented it in one whole file?
Answer:
If default-parameter is in the cpp-file, the main file does not see it as
it looks only into the header-file
But if the whole code is included in just one file, the default-value
can be found in the definition too.
To explain myself:
I considered answering my question, because it gives a better overview of the whole question and the question will now not appear as unanswered. After reading this, I think that it is the right way to do so.

Compiling C++ code on Linux. Need to use intel/icpc compiler. Error: "multiple definitions" related to inerited class

I am trying to compile a large C++ code (there are a few C files too) on a Linux cluster, having run it for some time on a Mac compiled with g++. On the cluster, I have to use either gcc/4.7.2 or intel/icpc (there is other software on the cluster that only works with those two compilers). I'm a newbie in dealing with compiling/linking problems, so no advice/tips is too simple.
I posted a question here a couple of days ago about a problem with using gcc/4.7.2. I haven't been able to resolve the problem, so now I'm trying icpc. Things have gone surprisingly well, but there is one problem I can't get past.
The problem is that I am getting errors related to "multiple definitions." These are associated with what seems to be a virtual function in a class that is inherited. I didn't write this code. There is a base class (Solver3), a derived class (Solver2), and another derived class (Solver1). The classes solve matrix equations. I can't tell which function is the problem because the error output is quite cryptic (see below; I have no function called "_fileno" and I can't find any generic definition of this term online). But the problem function is probably SolveWithSolver2 because it is the only function in the Solver1 class. I have no clue what could be wrong with it.
This is a bit over my head in terms of C++ knowledge and it is likely I'm making some beginner's mistake. For the past couple of days, I have used Google to search old forum posts. I have tried renaming what seems to be the problem function, I have tried inlining it. I get the same error. The code is very large and only a small part of it was written by me, so I can't post much of it. I will post what seems relevant and can add things if that would be helpful.
Here are the kinds of the errors I'm getting, starting from the first one (I have not posted all of the output):
/code/libraries/libsolver.a(Solver1.o): In
function _fileno(_IO_FILE*)': Solver1.cpp:(.text+0x0): multiple
definition of_fileno(_IO_FILE*)' main.o:main.cpp:(.text+0x1bc0):
first defined here
/code/libraries/libsolver.a(Solver2.o): In
function _fileno(_IO_FILE*)': Solver2.cpp:(.text+0x0): multiple
definition of_fileno(_IO_FILE*)' main.o:main.cpp:(.text+0x1bc0):
first defined here
/code/libraries/libsolver.a(Solver3.o):
In function _fileno(_IO_FILE*)':
Solver3.cpp:(.text+0x0): multiple definition of_fileno(_IO_FILE*)'
main.o:main.cpp:(.text+0x1bc0): first defined here
... And so on
Here is the Solver1 header code:
#ifndef SOLVER1
#define SOLVER1
#include "Solver2.h"
#include "ErrorHandler.h"
#ifdef SOLVER2
namespace code {
class Solver1 : public Solver2 {
public:
Solver1( );
//protected:
virtual void SolveWithSolver2( Matrix& A,
std::vector<double>& b,
std::vector<double>& x,
double tolerance );
private:
double pivot;
};
}
#endif
#endif
Here is the Solver2 header code:
#ifndef SOLVER2_H
#define SOLVER2_H
#include "Solver3.h"
#include "helper.h"
#ifdef SOLVER2
namespace code {
class Solver2: public Solver3 {
public:
Solver2 ();
//protected:
virtual void SolveWithSolver2(Matrix& A,
std::vector<double>& b,
std::vector<double>& x,
double tolerance) = 0;
private:
virtual void SolveEquation(Matrix& A,
std::vector<double>& b,
std::vector<double>& x,
stl_index unknowns);
double Residual(const Matrix& A,
const std::vector<double>& b,
std::vector< double >& x);
double Calculate(const SparseMatrix& A,
const std::vector<double >& b,
const std::vector< double >& x);
double residual;
};
}
#endif
#endif
Reply to Jakob:
Output of:
"grep _fileno" /usr/include/* -R
/usr/include/bits/dirent.h:#define d_fileno d_ino /* Backwards
compatibility. */ grep: warning:
/usr/include/c++/4.3/x86_64-suse-linux/32: recursive directory loop
/usr/include/dirent.h:#if (defined __USE_BSD || defined __USE_MISC) &&
!defined d_fileno /usr/include/dirent.h:# define d_ino d_fileno /*
Backward compatibility. */ /usr/include/lcms.h:# define fileno
_fileno /usr/include/libdwarf/libdwarf.h: Dwarf_Unsigned * /*ret_fileno*/, /usr/include/libio.h:#define _IO_DELETE_DONT_CLOSE
0x40 /* Don't call close(_fileno) on cleanup. */ /usr/include/libio.h:
int _fileno; /usr/include/linux/coda.h: u_int32_t d_fileno; /*
file number of entry */ /usr/include/linux/mtio.h: __kernel_daddr_t
mt_fileno; /* number of current file on tape */
/usr/include/sys/mtio.h: __daddr_t mt_fileno; /* Number of current
file on tape. */ /usr/include/X11/Xw32defs.h:#define fileno _fileno
Edit:
Using Jakob's suggestion about compiler flags (adding -Wl and -z) to my OPTIONS, my error output became much more clear; I got file names, line numbers, and particular errors.
I have now dealt with the problem, in the sense that I can compile that library. But to be honest, I don't really know why the compiler complained to begin with or why my solution worked. The problem involved preprocessor directives, which I confess I know little about. If anyone cares to speculate on what the issue was, it would be interesting to know. I have never run into needing ";" in preprocessor directives before.
This is what "fixed" things (sorry about the bold, large text; can't seem to turn that off):
define SOLVER_C_CONVERSION;
void __cdecl;
endif
This is what it looked like when it was a problem:
define SOLVER_C_CONVERSION void __cdecl
endif
So now that I've fixed that problem, I have one more library to deal with. It is throwing up all kinds of errors that g++ previously ignored. I may bother you all once more later on today if I can't solve them. I'm pretty sure the problem is with my makefile.
To solve the problem with the multiple definitions, the first step would probably be
tracking down the include dependencies,
using the '-M' or the '-H' compiler flag,
e.g. :
gcc -H {use correct include flags here: -I$( include path) } -c Solver1.cpp
This will show you a dependency tree (read top-down )
Then you could figure out, which of the files defines the _fileno symbol.
(e.g by using the grep command)
and finally you could try to understand, why _fileno is defined multiple times.
If you want a nicer dependency output, you could in general try to generate the include dependencies with doxygen.
Alternatively as a workaround you could use following link flags which will prevent the compilation process from failing in case of multiple definitions:
-Wl,-z,multiple

how to use not yet defined datatypes in a header?

I am programming on linux using g++ and I often encounter the problem that I need to use a class or data type in a header file which I define later, either at a later point in the header or in another header file.
For instance look at this header file:
class example
{
mydatatype blabla;
};
struct mydatatype
{
int blablainteger;
char blablachar;
};
This will give error because mydatatype is used before its defined
so usually I change it like this:
struct mydatatype; // <-- class prototype
class example
{
mydatatype *blabla; // <-- now a pointer to the data type
// I will allocate the data during runtime with the new operator
};
struct mydatatype
{
int blablainteger;
char blablachar;
};
Now it works. I could often just put the definition above, or include the header which is needed, but I don't want to include headers in a header or juggle with the definition order, it always gets messy.
The solution I showed usually works, but now I have encountered a new phenomenon. This time the datatype is not a class but a typedef, I cant use prototypes for a typedef and I don't want to use the actual datatype which the typedef incorporates.. it's messy too.
Is there any solution to this?
Firstly, the solution you've thought of (prototype and pointer), is unneeded, and slower than just implementing it without the pointer.
The "proper" solution for this, would be creating seperate headers for each type, and then include them in your other header. That way it will always be defined! You can even make them so that they include eachother.
However, if you've ever opened a .h file provided by g++, you've most likely seen this at the start of the header:
#ifndef SOMETHING_H
#define SOMETHING_H
// Code
#endif /* SOMETHING_H */
This is to solve the issue of types redefining themselves.
If they weren't there, and you included the header file multiple times, the types would be redefined, and an error would be thrown. This makes it so that the types are always present, but never included twice.
I hope that helps!
Place each class/type in it's own header file, and then include the relevant header file in other headers where you need it. Use an inclusion guard in each header e.g.:
// SomeHeaderFile.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
// code
#endif
I disagree that this is messy - it allows you have an organised structure to you project, it allows each class to operate independently of others and without worrying about order, and it's a good idea to place each class in it's own file anyway.
You could just define the class inside the other class like
template<class T>
class vertex {
private:
class edge {
public:
vertex<T> *to;
double weight;
edge() {
weight = INFINITY;
to = NULL;
};
} *paths;
T data;
unsigned nof_paths;
public:
vertex(T val) {
data = val;
paths = NULL;
nof_paths = 0;
}
void addPathTo(vertex<T>*&);
edge* getAllPaths() {
return paths;
};
};
Obviously this works for small classes... if your class is ENORMOUS you'll be better using separate header files like the other guys said.

difference between putting head file(.h) at the beginning and end of another file(.h or .cpp)

Recently , I was compiling libfacebookcpp on mac os. I found a strange usage which I can't understand.
There are two files, AuthorizedObject.hpp and List.hpp.
At the end of file AuthorizedObject.hpp,there is one line :#include "List.hpp".Now I compile successfully. But
when I move that line to the beginning, error occurs. The skeleton of the codes are:
//AuthorizedObject.hpp
class AuthorizedObject
{
public:
...
template<class TType>
void _GetConnection(const ::std::string& uri, List<TType> *list) const
{
LIBFACEBOOKCPP_CHKARG(list);
Json::Value value;
request_->GetResponse(uri, &value);
list->Deserialize(*this, value);
}
...
}
#include "List.hpp" //end
----------------------------------------------------------
//List.hpp
#include "AuthorizedObject.hpp"
class LIBFACEBOOKCPP_API List : public AuthorizedObject
{
private: // private classes
...
}
I guess if put that line(#include "List.h") at the beginning of AuthorizedObject.hpp, the two files include each other by circle. So the compiler don't know how to compile.But put that line at the end will solve this problem? Why? Thank you in advance.
The difference is in what order classes/functions/... are defined, for example:
#include "one.h"
void foo(bar &);
// Will result into:
class bar {};
void foo(bar&);
Which is valid code. On the other hand:
void foo(bar &);
#include "one.h"
// Will result into statements in different order:
void foo(bar&);
class bar {};
Which means using class bar before it was declared, thus error. You also may need to make sure that no declaration will be processed twice (UmNyobe already covered that partially):
#if !defined( MY_HEADER_INCLUDED_)
# define MY_HEADER_INCLUDED_
// Complete content goes here
#endif /* !defined( MY_HEADER_INCLUDED_) */
This way when you include file for the first time, MY_HEADER_INCLUDED_ won't be defined (contents will be "placed" inside the code). The second time (you will include it in the circle) MY_HEADER_INCLUDED_ will be defined and therefore complete body will be skipped.
You're right, that's the problem. You're wrong that the compiler doesn't know how to compile - it does, you're just using it wrong :).
The include guards at the beginning of AuthorizedObject.hpp (I assume it has include guards or a #pragma once directive) will define AUTHORIZED_OBJECT_H (or similar). After that, you include List.h, which in turn includes the header AuthorizedObject.hpp. Because the include guard macro was already defined, the definition of AuthorizedObject is skipped, so List doesn't know about the type, but is uses it, so you get the error.
If you move the include at the end, the definition of AuthorizedObject was already processed by the compiler, so using it inside List.h is valid.