473,471 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error: 'undefined reference' in g++ but gcc succeeded

Lu
[jxlu@edusrv compact]$ cat main.c
extern void asm_exit(int);

int main(int argc,char *argv[])
{
asm_exit(0);
}
[jxlu@edusrv compact]$ cat asm_exit.asm
; exit() by assembly: void asm_exit(int status)
; assemble: nasm -f elf asm_exit.asm

bits 32 ; 32bit mode

section .text ; code must resides in the ".text" in gcc
global asm_exit ; declare asm_exit() as a public function

asm_exit:
mov ebp, esp ; [ebp] points return address and [ebp+4] points 1st
argument
mov ebx, [ebp+4]; read status code
mov eax, 1 ; call sys_exit()
int 0x80 ; never returns

[jxlu@edusrv compact]$ cat Makefile
CC=gcc
CPP=g++
CFLAGS=-Wall -nostartfiles -e main
MACROS=

all: main++ main

main++: main.c asm_exit.o
$(CPP) $(MACROS) $(CFLAGS) -o main++ main.c asm_exit.o

main: main.c asm_exit.o
$(CC) $(MACROS) $(CFLAGS) -o main main.c asm_exit.o

asm_exit.o: asm_exit.asm
nasm -f elf asm_exit.asm

clean:
rm -f main main++ asm_exit.o
[jxlu@edusrv compact]$ make main
nasm -f elf asm_exit.asm
gcc -Wall -nostartfiles -e main -o main main.c asm_exit.o
main.c: In function `main':
main.c:6: warning: control reaches end of non-void function
[jxlu@edusrv compact]$ ./main
[jxlu@edusrv compact]$ make main++
g++ -Wall -nostartfiles -e main -o main++ main.c asm_exit.o
/tmp/ccGG2rjn.o: In function `main':
/tmp/ccGG2rjn.o(.text+0x16): undefined reference to `asm_exit(int)'
collect2: ld returned 1 exit status
make: *** [main++] Error 1
[jxlu@edusrv compact]$

Any suggestions?

Sincerely,
Lu


Jul 19 '05 #1
1 6830
Lu wrote:
[jxlu@edusrv compact]$ cat main.c
extern void asm_exit(int);


extern "C" void asm_exit(int);

C++ mangles function names (so it can differentiate between functions
with the same name but a different signature), while C doesn't. The
'extern "C"' modifier tells the compiler not to mangle the name of that
function.

--
"Codito ergo sum"
Roel Schroeven
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: skubik | last post by:
Anyone else have this problem compiling PHP 4.3.3 from source under Linux (I'm using Slackware 9.0)? Under PHP 4.3.2 I had no compilation problems at all, but now all of the sudden, compiling...
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: Hardy | last post by:
Hi, just come into the boost world. just the first.cpp in the program_options examples, with many link error... devc++4.9.9.2, gcc 3.4.2, can I get your opinions on this problem? thank you~ ...
1
by: Robert Fitzpatrick | last post by:
I am getting the following problem with trying to install from source PHP 4.3.6 with PG support on FreeBSD 5.2.1. I have PostgreSQL 7.4.2 recently upgraded from 7.3.x and it is up and running fine....
4
by: yogesh | last post by:
mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 compiler version ... #include <stdio.h> #include <mysql.h> #include <string.h> int main() {
4
by: sublimanized | last post by:
Hello all ... here is my problem. I just got the book "Teach Yourself C ++ in 21 Days" Setup: Fedora Core 6 - i386 GCC 4.1.1 Again, I am a complete newcomer to C++, and this is one of the...
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...
0
by: Benjamin Grieshaber | last post by:
Hi, I´m on SuSE 9.3 with xmlrpc-c and xmlrpc-c-devel installed (ver. 0.9.10) I tried to compile php with xmlrpc support and got the following errors: ...
3
by: singhPrabhat | last post by:
I have some .c and .h files in my directory. My aim is to compile the whole file. I am compiling each and every .c file by gcc -c file_name then I use gcc -o some_text file_name. I have found out...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.