Hi!

I'm working on a hobby OS. Almost every time I try to link my files I get a 'undefined reference to' error. As I've been trying to compile with different compilers, the errors generated by the linker changes. I have only been using different ports of the GNU compilers. I first started with GCC, then G++ and now MinGW. Currently, I just want to print a character on screen. The code is only in 9 lines and has worked with GCC, but it doesn't work with MinGW. As I'm developing a custom OS, I am developing everything from scratch. That means I'm not using any external libraries in my code.

Here's the code I'm currently trying to compile/link:

// main.cpp
int main(void)
{
    char *vidMem = (char*)0xB8000;
    
    vidMem[0] = 'A';
    vidMem[1] = 0x7;
	
	return 0;
}

I'm using Dev-Cpp together with MinGW.

The 'undefined reference to' that is generated when using MinGW is "undefined reference to '_alloca'" and "undefined reference to '__main'"

Recommended Answers

All 8 Replies

With me your code compiles (I'm using MinGW), but when running, your program will crash :)

With this low level of code it is probably better posed in the C forum than the c++ forum - I'd give it a try there.

A user-defined main() is not the first (and the only) function in the executable module. Have you ever heard about sturtup code? A console mode program has opened i/o streams - it's a job for startup code. It has a properly initialized memory heap - it's a job for startup code. There are lots of other "predefined" (hidden) objects - they are initialized by startup code. That's why main.cpp object module has references to library modules. If you suppress a linkage with C implementation libraries, you can't build an executable module (as you see)...

It's interesting: you want to execute main C function in a hobby OS but where is this OS code which can setup CPU modes and registers, initialize devices (hard drive and video card, for example;)), perform lots of other operations needed for loading and run this main module?

A user-defined main() is not the first (and the only) function in the executable module. Have you ever heard about sturtup code? A console mode program has opened i/o streams - it's a job for startup code. It has a properly initialized memory heap - it's a job for startup code. There are lots of other "predefined" (hidden) objects - they are initialized by startup code. That's why main.cpp object module has references to library modules. If you suppress a linkage with C implementation libraries, you can't build an executable module (as you see)...

It's interesting: you want to execute main C function in a hobby OS but where is this OS code which can setup CPU modes and registers, initialize devices (hard drive and video card, for example), perform lots of other operations needed for loading and run this main module?

Of course I know I need more than this little code for running an OS. I already have a boot loader which will set everything up, and then load this into memory. It has worked before with GCC.

With this low level of code it is probably better posed in the C forum than the c++ forum - I'd give it a try there.

I'll try that! Thanks!

With this low level of code it is probably better posed in the C forum than the c++ forum - I'd give it a try there.

I'll try that! Thanks!

Please do not start a new thread about this topic there, just ask a moderator to move this one to the C forum then :P

Please do not start a new thread about this topic there, just ask a moderator to move this one to the C forum then :P

Thread flagged.

With me your code compiles (I'm using MinGW), but when running, your program will crash :)

That's because it contains illegal code for *nix and MS-Windows operating systems. Its not even valid code for MS-DOS 6.X and older.

commented: Oh, that's why :P +9

try compile and linking on ubuntu. I try on ubuntu and successful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.