C/C++ mixed programming 본문

Programming/C++

C/C++ mixed programming

halatha 2011. 5. 24. 23:48
출처: http://yesarang.tistory.com/65
naming mangling
//	mixed.cpp
#include 

using namespace std;

double max(double a, double b)
{
	cout << "double max(double, double) called" << endl;
	if (a > b)	return a;
	else		return b;
}

int max(int a, int b)
{
	cout << "int max(int, int) called" << endl;
	if (a > b)	return a;
	else		return b;
}

int main(void)
{
	double	dm	=	max(23.7, 35.1);
	int	im	=	max(10,5);

	cout << "double max = " << dm << endl
		<< "int max = " << im << endl;
}

/*
$ g++ mixed.cpp
$ ./a.out
double max(double, double) called
int max(int, int) called
double max = 35.1
int max = 10
$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ nm ./a.out 
0000000000600e10 d _DYNAMIC
0000000000600fe8 d _GLOBAL_OFFSET_TABLE_
0000000000400a8f t _GLOBAL__I__Z3maxdd
0000000000400b98 R _IO_stdin_used
                 w _Jv_RegisterClasses
0000000000400934 T _Z3maxdd
0000000000400988 T _Z3maxii
0000000000400a4f t _Z41__static_initialization_and_destruction_0ii
*/

//	mixed.cpp
#include 

using namespace std;

double max(double a, double b)
{
	cout << "double max(double, double) called" << endl;
	if (a > b)	return a;
	else		return b;
}

int max(int a, int b)
{
	cout << "int max(int, int) called" << endl;
	if (a > b)	return a;
	else		return b;
}

namespace test	{
	char max(char c1, char c2)
	{
		cout << "char test::max(char, char) called" << endl;
		if ( c1 > c2 )	return	c1;
		else			return	c2;
	}
};

using namespace test;

int main(void)
{
	double	dm	=	max(23.7, 35.1);
	int	im	=	max(10,5);
	char	cm	=	max('a', 'z');

	cout << "double max = " << dm << endl
		<< "int max = " << im << endl
		<< "char max = " << cm << endl;
}
/*
$ g++ mixed.cpp
$ ./a.out 
double max(double, double) called
int max(int, int) called
char test::max(char, char) called
double max = 35.1
int max = 10
char max = z
$ nm ./a.out | grep max
0000000000400b82 t _GLOBAL__I__Z3maxdd
00000000004009a4 T _Z3maxdd
00000000004009f8 T _Z3maxii
0000000000400a34 T _ZN4test3maxEcc
*/
Comments