# What causes "Linker error" in Dev C++



## MSST8DOG

How do you go about determining what causes linker errors when you try to compile a program in Dev C++?

I am writing a program as requirement for CSC 102 class at the university. 

I am not asking how to fix my code, but just some direction on how to find what causes the error.

I had first thought that I failed to define a class or function that was declared, however, after going through all the .cpp and .h files, I saw all classes / functions defined.

Thanks, 
Lee


----------



## Ninjaboi

Hello MSST8DOG, welcome to TSF!

There are many different types of linker errors you will receive in your programming experience, but the main three you will see are:

*1.* Linker errors related to not linking to a library of which you utilize a function of in your program.

- Resolved by linking to the library and giving your compiler and linker the direct location of the file you are linking to.

*2.* Linker errors related to linking to an incompatible version of a library of which you utilize a function of or linking to a library the conflicts with another library you're linking to.

- Resolved by using the version of which you use that function when the code was created, and/or set your project linker parameters to settings that allows the two conflicting libraries to comply with one another.

*3.* Linker errors related to multiple definitions of a file ( say #include <iostream> if it didn't have header guards ).

- Resolved by using proper header guards on included files ( if you cannot change the source of the file for any reason, you should try to include it only once in your project ). Some people put all their includes into a single header file they create, then put header guards around the file they created when they want to perform a single include throughout their application.

The best way to diagnose a linker error is to read it in full and break it apart to see what it's telling you. Let's take two common linker errors for an example.



> Example /tmp/cc2Q0kRa.o: In function `main': /tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)' collect2: ld returned 1 exit status
> Meaning
> Your code called the function Print, but the linker could not find the code for it in any .o file
> Usual Causes
> You forgot to link the .o file that contains the function
> You misspelled the name of the function
> You spelled the name of the function correctly, but the parameter list is different in someway





> Example /usr/lib/crt1.o: In function `_start': /usr/lib/crt1.o(.text+0x18): undefined reference to `main' collect2: ld returned 1 exit status
> Meaning
> This is a special case of "undefined reference"
> Usual Causes
> You compiled a .cpp that does not contain main( ), but forgot to use the -c switch. When compiling .cpp files that do not contain main( ), use the command line *g++ -ansi -Wall -c myfile.cpp*


*Source(s):*

Dev-C++... Linker Errors - GameDev.net
Common C++ Compiler and Linker Errors
Dealing with Compiler and Linker Errors - Cprogramming.com


----------



## MSST8DOG

Ninjaboi said:


> Hello MSST8DOG, welcome to TSF!
> 
> There are many different types of linker errors you will receive in your programming experience, but the main three you will see are:
> 
> *1.* Linker errors related to not linking to a library of which you utilize a function of in your program.
> 
> - Resolved by linking to the library and giving your compiler and linker the direct location of the file you are linking to.
> 
> *2.* Linker errors related to linking to an incompatible version of a library of which you utilize a function of or linking to a library the conflicts with another library you're linking to.
> 
> - Resolved by using the version of which you use that function when the code was created, and/or set your project linker parameters to settings that allows the two conflicting libraries to comply with one another.
> 
> *3.* Linker errors related to multiple definitions of a file ( say #include <iostream> if it didn't have header guards ).
> 
> - Resolved by using proper header guards on included files ( if you cannot change the source of the file for any reason, you should try to include it only once in your project ). Some people put all their includes into a single header file they create, then put header guards around the file they created when they want to perform a single include throughout their application.
> 
> The best way to diagnose a linker error is to read it in full and break it apart to see what it's telling you. Let's take two common linker errors for an example.
> 
> 
> 
> 
> 
> *Source(s):*
> 
> Dev-C++... Linker Errors - GameDev.net
> Common C++ Compiler and Linker Errors
> Dealing with Compiler and Linker Errors - Cprogramming.com


Thanks Ninjaboi, this solved a portion of my problem.

I made some code changes and used g++ -ansi -Wall -c myfile.cpp with compilation success. 

We use DEV C++ in class though and it won't compile with DEV (still get Linker errors) as in: [Linker error] undefined reference to 'Class::member(std::string)'

Is this because I haven't defined the members of the class? I believe I have the class and all members correctly defined.

Have a great day


----------



## Ninjaboi

If you feel confident that you have defined the class and its members correctly, it must be how you're referencing it ( hence why it says "undefined reference" ). Could you post the code if it's not very large? If it's large, you can just take a snippet of the code and display it as long as it's relevant to the error. Also, could you provide the exact linker error you're receiving?

I ask this due to there being so many possible causes. It could be:

- You created a static member and it was referenced without being linked.
- Member not being defined.
- Member not being defined in the right location ( defined after it was being used ).
- Et cetra.


----------



## MSST8DOG

I will look at that again. The problem is, if I give the code/exact error message, it would probably violate the usage agreement rules, i.e. homework, etc... as there is no way I could post it without showing up as homework.

Thanks for the assistance. I will look at the static and reference stuff and see if I did that or maybe not defining a member in the right place, etc..

Go St8


----------



## Ninjaboi

You said it compiled perfectly in G++, which could mean that it is how you setup Dev-C++ on your machine. Either that, or it's how you setup your project in Dev-C++. If it not only compiles but runs using G++ too, I'd say that would be a viable assumption. You might check out your project and Dev-C++ setup to ensure that everything has been setup correctly.

It wouldn't exactly be violating the rules, as the rules prohibit users from asking for assistance with homework. We can help you understand topics that can apply to homework, but we can't help you with the problem you may have on the homework ( if I worded that correctly ). The fact that this error could have so many things to cause it, narrowing it down would be a big help in diagnosing what we can teach you. I'm glad that you're concerning with the rules, as several users don't even read them.


----------



## MSST8DOG

Ok, after looking, it really doesn't "compile" properly in g++. It does create a file with .o extension which I haven't seen before, but it doesn't generate an .exe file. I was too focused on it not giving an error and trying to figure out why it wasn't compiling in Dev that I never looked. "sheepish grin", Now I have more to look for...

I will see if I can copy the code and maybe someone can tell me which area I'm deficient in. I will get back to the code sometime after lunch today.


----------

