smorf
smorf

Reputation: 23

Linking GCC(G++) ASM object with .cpp objects not working(undefined reference to...)

I have two functions that I need to write directly on ASM, as GCC will include pre/post call codes when compiling those functions, and I don't want that.

Assume that as a immutable: I have to compile two functions directly on asm.

Two functions are implemented as:

    .intel_syntax 
    .text
    .globl  Func1
    .def    Func1;  .scl    2;  .type   32; .endef    
Func1:  
    push 1
    push esp     
    mov eax, 0xDEADBEEF
    call eax 
    ret   

    .globl  Func2
    .def    Func2;  .scl    2;  .type   32; .endef
Func2:
    ret

I'm generating objects using: g++ -c asm.S -o asm.o . And that works! I have a asm.o with no error/warnning.

But, when compiling all codes as:

g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Test.exe ../Test/asm.o release/main.o release/mainwindow.o release/funcs.o release/qrc_a.o release/moc_mainwindow.o  -lpsapi -lOleAut32 -lmingw32

I got the follow error:

release/funcs.o:funcs.cpp:(.text+0x5c4): undefined reference to `Func2()'
release/funcs.o:funcs.cpp:(.text+0x5d0): undefined reference to `Func1(HINSTANCE__*)'
release/funcs.o:funcs.cpp:(.text+0x6a5): undefined reference to `Func1(HINSTANCE__*)'

So the linker can't find Func1 and Func2 objects... but I included asm.o(at ../Test/asm.o) on compiling, so I don't know what's wrong :(.

Thanks for any help o/

Btw: I'm using g++ (GCC) 4.8.1 from MinGW, so I'm at Windows environment.

Upvotes: 0

Views: 486

Answers (2)

mksteve
mksteve

Reputation: 13085

The C++ compiler is looking for name mangled functions (with calling parameter types).

extern "C" int func1(); and maybe asm => _func1.

Upvotes: 0

Jonathan Wakely
Jonathan Wakely

Reputation: 171413

The linker is looking for a function Func1() not just a symbol name, Func1, so it will be looking for a symbol matching the mangled name of that function. You haven't defined any such symbol, you have defined Func1 instead, without a mangled name.

You need to declare the functions as extern "C" in the C++ file so that the linker just looks for Func1 which is what you have defined in the asm.

Upvotes: 1

Related Questions