0

i have a problem with some code, which is distributed over different files. I have a main.cpp where the methods are called. In this main.cpp I'm including a "deklarationen.h", which declares the methods with their signatures. For example void "average(float, float&);"

The body of this method is in a "average.cpp". But when i try to compile my main.cpp i get the following error code "\main.o:main.cpp|| undefined reference to `average(float, float&)'".

All the files are in the same directory, and as I said I'm including the header file in my main.cpp .

Hint: I'm using Code::Blocks with the ming32-gpp compiler.

Am I doing something terribly wrong, or is it a compiler bug or smth?

Greetings

Code: Main.cpp

#include "deklarationen.h"
#include <iostream>
using namespace std;
int main ()
{
//body of the function
average(b,mittel);
}

Code: deklarationen.h (same folder)

#ifndef _DEKLARATIONEN_H_
#define _DEKLARATIONEN_H_
float a;
void einlesen(float &a, float &b);
void average(float, float &avg);
void quotient(float, float&);
void produkt(float b, float& quot);
float summe(float);
extern float differenz(float);

#endif  /* _DEKLARATIONEN_H_ */

Code: average.cpp (still same folder)

void average(float b,float &avg)
{
    avg = (a+b)/2;
}
Shentoza
  • 35
  • 7
  • did you compile the `average.cpp`? Or better: share the build command you are running. – Theolodis May 27 '14 at 11:28
  • "mingw32-g++.exe -c main.cpp -o main.o" "mingw32-g++.exe -o main.exe main.o" that are the build commands my IDE is executing PS: @πάνταῥεῖ i think i know what undefined references in general are. but i dont know why he isnt able to link the files that are in the same folder. – Shentoza May 27 '14 at 11:40
  • 1
    @user3679491 _'i think i know what undefined references in general are'_ Apparently not! You're missing to compile and link `average.cpp`. It doesn't matter if it's _in the same folder_, there's nothing passed to the compiler/linker automatically. – πάντα ῥεῖ May 27 '14 at 11:44
  • @πάνταῥεῖ i said "i think" then i'm really sorry bout that. The thing is, the method average is using the global variable a, which can't be compiled at the moment. When i include the "deklarationen.h" for the variable a, its throwing a undefined reference to 'WinMain@16' since it has no main method. – Shentoza May 27 '14 at 11:52
  • @user3679491 `mingw32-g++.exe -c main.cpp average.cpp -o main.o` should do the trick. – Theolodis May 27 '14 at 11:53
  • @user3679491 Read all of the answers in the link I proposed you. There's also one for your actual problems! – πάντα ῥεῖ May 27 '14 at 11:54
  • @Theolodis nope, `fatal error: cannot specify -o with -c, -S or -E with multiple files.` Anyways i don't think its a code problem, but a linker / compiler problem. It compiled perfectly well(all files that are required were compiled "on it's own"), before i installed a different mingw32, and altered some of the environment variables. But...regarding the error codes it doesn't seem like that's the problem – Shentoza May 27 '14 at 12:06
  • @user3679491 yep, sorry, remove the -c option. And no, no compiler could compile your code without having `average.cpp` compiled. A workaround could be to place all content of `average.cpp` into the header `deklarationen.h`, but that would be dirty. – Theolodis May 27 '14 at 12:10

1 Answers1

0

When you compile c++ code, the following (simplified) steps occur:

  1. precompilation (substitution of includes, macros..)
  2. object file generation.
  3. linking.

Step 1 is working fine, or you would be getting a commpiler error. You have a problem with either steps 2 or 3 or both. An "undefined refernce" error means that the linker is not finding a reference to the compiled version of your method.

First you should check if all of your object files are being built. You can tell this by looking at the compiler output and seeing if you see any messages like g++ filename .. filename.o, or by looking in your build directory to see if there is an *.o file for every *.cpp file you have.

The final step is linking, in which the linker joins all of the .o files into an exectuable.

I don't work with CodeBlocks, but probably your problem is you need to instruct code blocks to include the files which contain the missing references into the build.

Alternatively you can hand-write a makefile, learn cmake, or compile from the command line, all useful skills

I saw that you posted the build command that codeblocks is executing:

"mingw32-g++.exe -c main.cpp -o main.o" "mingw32-g++.exe -o main.exe main.o

Here only main is being compiled, and no other files are either compiled or linked.

Spacemoose
  • 3,856
  • 1
  • 27
  • 48
  • exactly. I think for myself that its the linking thats causing trouble, since i have for every .cpp file an object file. And as i said it used to work. But somehow it doesnt get linked. (When i try to compile a different code, which only uses 1 .cpp, all is fine) I even tried to reinstall code::blocks to reinstall the built-in linker/compiler, because, as I said. It used to work fine. – Shentoza May 27 '14 at 12:47
  • PS: http://pastebin.com/3d8dh5JL thats the error code when i try to `mingw32-g++.exe -o main.exe main.o average.o differenz.o einlesen.o produkt.o quotient.o summe.o` – Shentoza May 27 '14 at 12:51
  • I looked at your compile command, and it is not generating any .o files other than main.o. The first line in quotes generateds main.o, the second says to make main.o into an executable. It should also have average.o in the list of .o files to be linked. In the posted command average.o is neither compiled nor linked. This indicates that you used to have average.cpp in your build, and no longer do. Try a clean build, and see if average.o gets regenerated. Based on your description, I guess you had average.cpp in your list of build targets, and now no longer do. – Spacemoose May 27 '14 at 12:53