4 Replies - 3443 Views - Last Post: 18 December 2012 - 01:09 PM

#1 Jiro_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

trouble calling C++ functions

Posted 13 December 2012 - 11:42 AM

I wrote a small toy kernel in C++, and a small graphics library.
now I wanted to call this function from assembly, but that doesn't really work out.
I can compile the cpp en asm files into object code, but when I link them, it keeps saying
'error, undefined reference to _main'
the assembly code looks like this:
;loader.asm
[BITS 32] ;protected mode
global start
extern _main ;this is our C++ code
start:
  call _main ;call the int main(void) code in C++ source
  cli ;clear interrupts
  hlt ;halt the CPU


the kernel cpp file looks like this:
#include "video.h"
int main(void)
{
    video vid; //local variable
    vid.Write("hello world!");
}


and since I'm using a linker file, I'll give that one as well:
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 = .;
}



I would really like to know why I can't call '_main'

Is This A Good Question/Topic? 0
  • +

Replies To: trouble calling C++ functions

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: trouble calling C++ functions

Posted 13 December 2012 - 05:13 PM

this could be a few things

1. C++ mangles object names so that overloads with the same name and different types can be linked; if you look at the symbols in your object code I bet you don't see _main but rather "_main" plus a bunch of stuff around it denoting type information. C dosn't do this so you can use an extern "C" prefix to make it externalize the function the way C would. I'm not sure weather you can do this with main but you can defiantly do it with a function that calls main. I would try it with main first and see if it works.

extern "C" int main() {
   //...here you go
}



you could also I suppose try and figure out how main is being mangled but that's highly undefined and probably not the easiest way to do it.

2. not all compilers I've used add the underscore. for instance MinGW adds it but actual GCC Linux implementations don't. So depending on the compiler it could just be 'main' and not '_main'

This post has been edited by ishkabible: 13 December 2012 - 05:15 PM

Was This Post Helpful? 1
  • +
  • -

#3 Jiro_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

Re: trouble calling C++ functions

Posted 14 December 2012 - 12:46 PM

View Postishkabible, on 13 December 2012 - 05:13 PM, said:

this could be a few things

1. C++ mangles object names so that overloads with the same name and different types can be linked; if you look at the symbols in your object code I bet you don't see _main but rather "_main" plus a bunch of stuff around it denoting type information. C dosn't do this so you can use an extern "C" prefix to make it externalize the function the way C would. I'm not sure weather you can do this with main but you can defiantly do it with a function that calls main. I would try it with main first and see if it works.

extern "C" int main() {
   //...here you go
}



you could also I suppose try and figure out how main is being mangled but that's highly undefined and probably not the easiest way to do it.

2. not all compilers I've used add the underscore. for instance MinGW adds it but actual GCC Linux implementations don't. So depending on the compiler it could just be 'main' and not '_main'

thanks a lot! I hadn't tought of that yet
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: trouble calling C++ functions

Posted 14 December 2012 - 07:57 PM

did it work?
Was This Post Helpful? 1
  • +
  • -

#5 Jiro_  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 66
  • Joined: 27-August 12

Re: trouble calling C++ functions

Posted 18 December 2012 - 01:09 PM

View Postishkabible, on 14 December 2012 - 07:57 PM, said:

did it work?


yes, thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1