Page 1 of 1

moving code from c to C++ gives linker error

Posted: Sat Mar 17, 2012 1:21 pm
by bartexsz
I am learning OS developmebt from JamesM's kernel development tutorial.

Recently i decided to rewite screen handling as object. Unfortunately i can't now link it, linker gives me:

Code: Select all

main.o: In function `main':
main.cpp:(.text+0x7e): undefined reference to `_Unwind_Resume'
main.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
My makefile:

Code: Select all

SOURCES=boot.o main.o common.o screen.o

CPPFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector
LDFLAGS=-Tlink.ld
ASFLAGS=-felf

all: $(SOURCES) link 

clean:
	-rm *.o kernel

link:
	ld $(LDFLAGS) -o kernel $(SOURCES)

.asm.o:
	nasm $(ASFLAGS) $<
link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
    .text 0x100000 :
    {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
    }

    .data :
    {
    data = .; _data = .; __data = .;
    *(.data)
    . = ALIGN(4096);
    }
    
    .bss :
    {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}

Re: moving code from c to C++ gives linker error

Posted: Sat Mar 17, 2012 2:48 pm
by xenos
You probably wish to disable exception handling: C++ Bare Bones

Re: moving code from c to C++ gives linker error

Posted: Sat Mar 17, 2012 5:07 pm
by Combuster
Nor are you using the GCC Cross-Compiler

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 5:03 am
by bartexsz
Thanks for help , i had to simply add extern "C" to main function, as in c++ Bare Bones

EDIT: It was enough , but only for main. I started implementing gdt, and these errors are back.
I tried to use gcc-cross compiler, but I don't know how to put $TARGET to makefile to make it works.

EDIT2: Ok, I added -ffreestanding , now it works, however, now i get:

Code: Select all

boot.o main.o common.o screen.o gdt_entry.o descrtables.o gdtptr.o link
make: boot.o: Command not found
It worked earlier. What can be wrong?

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 9:24 am
by invalid
You forgot $(CC) in your Makefile and it tries executing "boot.o"?

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 12:28 pm
by bartexsz
Ok, thx.

One more question, how can I implement c++ class method in external asm file?

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 1:08 pm
by bartexsz
thx, I used g++ with -S parameter to find mangled name. Is there any way to force compiler to not mangle name?

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 1:09 pm
by brain
Yes. extern "C" void funcname() { }

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 1:29 pm
by bartexsz
It doesn't work for functions which belongs to class

Re: moving code from c to C++ gives linker error

Posted: Sun Mar 25, 2012 1:58 pm
by bluemoon
To access class member you either dirty hack the mangling/ABI or write an extern C wrapper function. Either way it is not elegant, and the need to access class member in assembly is usually due to unnecessary optimization or design problem.

We have a thread talk about this recently.

Re: moving code from c to C++ gives linker error

Posted: Sat Mar 31, 2012 11:07 am
by bartexsz
Not all can be implemented in C++ :)

Actualy it is GDT implementation, in which now works all, but flushing. Unfortunately flushing hangs all.

If someone could check this, here is my github project: https://github.com/bartexsz/Freeq-OS

descrtables.cpp contains function in which i'm calling flush() and gdt.asm implemetation of this function

Re: moving code from c to C++ gives linker error

Posted: Sat Mar 31, 2012 11:44 am
by bluemoon
As berkus mentioned, you either call assembly function from C++ member, or if speed is critical you do inline assembly.

By hacking into mangling you lock yourself into a specific compiler, and do not surprise if your code breaks on next year's gcc.

Re: moving code from c to C++ gives linker error

Posted: Sat Mar 31, 2012 12:18 pm
by Owen
If you're willing to use a GCC extension, you can define the symbol name using the 'asm("SymbolName")' directive

Re: moving code from c to C++ gives linker error

Posted: Sun Apr 01, 2012 5:38 am
by bartexsz
I forgot to say, I solved compiling/linkage error. My only problem now is flushing which hangs all, despite fact that it's called nearly exactly like in JamesM's kernel development tutorial, what shows my code on github. (Link in my previous post).