News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Linking object files does not work correctly

Started by Querente, October 14, 2005, 12:02:13 PM

Previous topic - Next topic

Querente

I am starting with CodeBlocks (using DevCPP before that) and started a project (opengl-based). The project has several source files and several header files.  All the files compiles correctly during the build ,however during linking I am receiving an error.

=== Log ===
mingw32-g++.exe  -LD:/CodeBlocks/lib -o D:/CodeBlocks/projects/OpenGL.exe D:/CodeBlocks/projects/main.o D:/CodeBlocks/projects/se_mem.o D:/CodeBlocks/projects/se_win.o D:/CodeBlocks/projects/log.o     -lopengl32 -lglu32 -lgdi32 -luser32 -lkernel32  -mwindows
D:/CodeBlocks/projects/main.o:main.cpp:(.text+0x7): undefined reference to `Start()'
collect2: ld returned 1 exit status
make.exe: *** [D:/CodeBlocks/projects/OpenGL.exe] Error 1
Process terminated with status 1 (0 minutes, 0 seconds)
=== End of Log ===

The undefined reference to Start() is the problem.  The name is known in the header file se.h and the function itself in se_win.c (se_win.o after compilation).  The problem, I am getting this error while all should be correct.  I havent had any trouble with DevCpp compiling and linking object files (it was all handled automatically, just like it should work with CodeBlocks)

mandrav

The only thing I see that might affect linking, is that you 're using C files (not C++) and try to link them using the C++ linker. I 'm not sure this is a problem but to check, go to "Settings->Compiler->Programs", set the linker to "mingw32-gcc.exe" and see if it works...
Be patient!
This bug will be fixed soon...

Querente

I am afraid that didnt work, although it did change the program compiler/linker ,but I received the same error nonetheless.

It is also true that most of my files are C ,but the main.cpp is CPP (well, not in function, just in name, as CB created it in the opengl template).


Querente

I renamed the main.cpp to main.c and re-added it to project.  It actually works this time. Perhaps it has something to do with calling conventions, but that is something I still need to read up on.

I guess I have to either, keep all my source files to pure C, or pure CPP..unable to mix them. This is however, rather...irritating. Is there another solution ?  Other compiles/IDE's dont have this problem (like DevCpp)..this might be because they force all sources to be compiled as c++..not sure if there is an option like that in CB as I just started with it.

mandrav

Quote from: Querente on October 14, 2005, 12:37:07 PM
Other compiles/IDE's dont have this problem (like DevCpp)..this might be because they force all sources to be compiled as c++..not sure if there is an option like that in CB as I just started with it.

Well, in other IDEs you must select in the project options whether this is a C or C++ project. In C::B you don't have to. It compiles files based on their type.
This is the "correct" behaviour because it allows you to select per-file how to compile it (contrary with other IDEs).
You could also try setting the compiler var to CC in main.cpp's properties. This would compile it with the C compiler...

QuoteThis is however, rather...irritating.

Depends on how you look at it. In general, it's not wise to mix and match C and C++ files. If you have to, put them in different libs. And don't forget that this IDE has an open architecture which will allow it (at some point :P) to work with languages other than C/C++. How would a "Project is C/C++" option in project options seem to someone working with Python (for example), when that time comes? ;)
Be patient!
This bug will be fixed soon...

Querente

Yeah, I know CB is more correct , but for someone lazy as well, it is a slight irritation :).  I mean ,I am so lazy I even dont put "extern C {  } ", linker flag in my source files when I mix them. (Which is one of the problems I guess).

But thanks for the help ,atleast I know where the problem lies and how to deal with it :)

mandrav

Be patient!
This bug will be fixed soon...

Urxae

Quote from: Querente on October 14, 2005, 01:08:56 PM
I mean ,I am so lazy I even dont put "extern C {  } ", linker flag in my source files when I mix them. (Which is one of the problems I guess).

I was just about to post that was the problem here. If you want to use a C function from C++, you need to have it declared extern "C" (while compiling C++). The usual way to do this is to write headers like this:
#ifndef HEADERNAME_H
#define HEADERNAME_H

/* Some #includes here perhaps */

#ifdef __cplusplus
extern "C" {
#endif

/* C declarations here */

#ifdef __cplusplus
}
#endif

#endif // HEADERNAME_H


If you don't do this, the C++ compiler will create a call to the mangled name of the function instead of to the "C name". Since the function is actually compiled with a C compiler, it'll generate the function with the unmangled C name. Which means the linker will be looking for the function under the wrong name.