473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_comp_solver':
/home/gianx80/Documenti/equa.s:81: undefined reference to `rad'


So, how can I compile my program without errors?
Feb 2 '10 #1
5 6545
Banfa
9,065 Expert Mod 8TB
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
Feb 3 '10 #2
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?
Feb 3 '10 #3
Banfa
9,065 Expert Mod 8TB
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.
Feb 3 '10 #4
donbock
2,426 Expert 2GB
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.
Feb 4 '10 #5
johny10151981
1,059 1GB
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)
Feb 4 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
1
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the...
1
by: Dom | last post by:
I'm new to c++. Just started learning it 24 hours ago. Am running into a compile problem. Please, no one waste the effort telling me to google it. I've been researching it for quite a while with no...
2
by: David Muoio | last post by:
I am trying to validate an XML file against an XSD that is stored in the assembly as an embedded resource. I can get it to work as long as the XSD does not include other XSDs. After a fair amount...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
3
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
5
by: rcoco | last post by:
Hi, I have this program that I'm trying to compile and it flags this error: The type or namespace name 'src' could not be found(are you missing a using directive or an assembly reference?) I've...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
14
by: Monty | last post by:
Hello, I have created a solution which has both a web UI and a winform UI, the latter is just for administrators. The Web UI (a Web Application Project) and the winform project both...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.