User forums > Help

Linking object files does not work correctly

(1/2) > >>

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

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.

--- End quote ---

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.
--- End quote ---

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? ;)

Navigation

[0] Message Index

[#] Next page

Go to full version