c++ and assembly: undefined reference to

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gianx80
    New Member
    • Nov 2009
    • 6

    c++ and assembly: undefined reference to

    Hi!
    I'm writing a little equation solver for an exam ... it has not to be perfect or mathematically rigorous. With this program I have only to show to my professor that I can write some simple program in Assembly and C++ language. In other words, I have only to show if I have understood the basis of Assembly language. I'm referring to x86 Assembly Language.
    Here's the source code: there are some c++ classes and assembly functions:

    (I cannot post all the source code because it's too long for the posting form, I'm uploading it to my dropbox folder, please download it).

    FILE 1: http://dl.dropbox.com/u/3371514/class_razionali _debug.cpp
    FILE 2: http://dl.dropbox.com/u/3371514/equa.s
    FILE 3: http://dl.dropbox.com/u/3371514/rad.cpp

    It's obvious that this source cod isn't complete (I'm going to finish it during the next days) but it should compile. It doesn't. I'm under linux (ubuntu) and I'm trying to compile it using these commands:

    g++ -c -o equa_s.o equa.s -g3 -m32
    g++ -c -o rad.o rad.cpp -g3 -m32
    g++ -c -o equa_c.o class_razionali _debug.cpp -g3 -m32
    g++ -o equa.out equa_s.o equa_c.o rad.o -g3 -m32

    But when I try to link all the object files I get this error message:
    equa_s.o: In function `second_grade_c omp_solver':
    /home/gianx80/Documenti/equa.s:81: undefined reference to `rad'


    So, how can I compile my program without errors?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The C++ compiler mangles file names to include some reference to the parameters the function takes, this is to enable function overloading. However it makes writing assembler functions a little hard because you would need to mangle your functions in the same manor as the C++ compiler for the linker to be able to work (resolve all externals). This would be a very hard job.

    Luckily there is a work around. Any C++ function can be declared has having C calling convention. In this converntion the names are not mangled (but the functions can't be overloaded either), it exists to support multi-language programming such as C++/C C++ Assembler etc.

    You might want to start by reading this which gives some explaination of how to mix C++ and C in a program. Mixing C++ and assembler is very similar since C is only a half step up from assembler.


    So what you actually need to do is

    Declare any assembler functions called from the C++ code as having C calling convention using extern "C" in the C++ code. Declare any function defined in C++ and called from assembler as have C calling convention using extern "C". This will stop the C++ name mangling and allow the linker to correctly locate and link the symbols.

    Try Googling these phases
    extern "C"
    extern "C" assembler

    Comment

    • gianx80
      New Member
      • Nov 2009
      • 6

      #3
      I solved the problem. I used the extern "C" and merged the rad.cpp file with the class_razionali _debug.cpp file.
      If I mantain the three files organization, the source code won't link. Why does this happen?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Hard to say without seeing the 3 files and the link errors but most likely you compiled your C++ source files with the functions declared in different ways.

        That is what headers are for you put your declarations in the header and include the header everywhere so all code sees the same declaration.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Does C++ support the asm directive? If so, you could write your assembly code in a .cpp file. Retain the C++ function wrappers but fill the function bodies with assembly language.

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            all I can Recall IS C++ supprot asm directive.
            (Funny thing in university: our teacher was suppose to teach us C not c++. They were doing that too. But after a year I discovered that we were doing program in *.cpp files and we were also using lots of syntax that is only supported by C++ :s)

            Comment

            Working...