Author Topic: Linking object files does not work correctly  (Read 4277 times)

Offline Querente

  • Single posting newcomer
  • *
  • Posts: 4
Linking object files does not work correctly
« on: October 14, 2005, 12:02:13 pm »
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)

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: Linking object files does not work correctly
« Reply #1 on: October 14, 2005, 12:22:40 pm »
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...

Offline Querente

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking object files does not work correctly
« Reply #2 on: October 14, 2005, 12:32:25 pm »
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).


Offline Querente

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking object files does not work correctly
« Reply #3 on: October 14, 2005, 12:37:07 pm »
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.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: Linking object files does not work correctly
« Reply #4 on: October 14, 2005, 12:56:05 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...

Quote
This 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...

Offline Querente

  • Single posting newcomer
  • *
  • Posts: 4
Re: Linking object files does not work correctly
« Reply #5 on: October 14, 2005, 01:08:56 pm »
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 :)

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: Linking object files does not work correctly
« Reply #6 on: October 14, 2005, 01:10:59 pm »
No problems, I 'm glad you got it working :)
Be patient!
This bug will be fixed soon...

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Linking object files does not work correctly
« Reply #7 on: October 14, 2005, 02:42:06 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:
Code: [Select]
#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.