Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- Book
- AI
- France
- erlang
- web
- Programming
- Python
- Malaysia
- history
- hadoop
- MySQL
- ubuntu
- django
- QT
- Italy
- essay
- Book review
- comic agile
- program
- Software Engineering
- programming_book
- Artificial Intelligence
- management
- leadership
- hbase
- Kuala Lumpur
- RFID
- Java
- Linux
- agile
Archives
- Today
- Total
C/C++ mixed programming 본문
2011/05/24 - [Programming/C++] - C/C++ mixed programming
not to use name mangling
C library 작성시 나중에 C++에서 활용될 가능성을 미리 염두해 두고, 헤더 파일 처음과 마지막에 extern "C" {}가 조건부 컴파일되게 한다
not to use name mangling
C library 작성시 나중에 C++에서 활용될 가능성을 미리 염두해 두고, 헤더 파일 처음과 마지막에 extern "C" {}가 조건부 컴파일되게 한다
// externc.cpp
#include <iostream>
using namespace std;
extern "C" int max(int a, int b)
{
if ( a > b ) return a;
else return b;
}
int main(void)
{
int im = max(100, 30);
cout << "int max = " << im << endl;
}
/*
$ g++ -o externc externc.cpp
$ ./externc
int max = 100
$ nm externc | grep max
000000000040097a t _GLOBAL__I_max
00000000004008d4 T max
*/
// max.h
int max(int a, int b);
// max.c
int max(int a, int b)
{
if ( a > b ) return a;
else return b;
}
// cppmain.cpp
#include <iostream>
#include "max.h"
using namespace std;
int main(void)
{
int im = max(100, 30);
cout << "int max = " << im << endl;
}
/*
$ gcc -c max.c
$ g++ -c cppmain.cpp
$ g++ -o cppmax cppmain.o max.o
cppmain.o: In function `main':
cppmain.cpp:(.text+0x13): undefined reference to `max(int, int)'
collect2: ld returned 1 exit status
*/
// max.h
#ifdef __cplusplus
extern "C" {
#endif
int max(int a, int b);
#ifdef __cplusplus
}
#endif
// max.c
// same as above
// cppmain.cpp
// same as above
/*
$ gcc -c max.c
$ g++ -c cppmain.cpp
$ g++ -o cppmax cppmain.o max.o
$ ./cppmax
int max = 100
*/
// cmain.c
#include <stdio.h>
#include "max.h"
int main(void)
{
int im = max(100, 30);
printf("int max = %d\n", im);
}
/*
$ gcc -c cmain.c
$ gcc -o cmax cmain.o max.o
$ ./cmax
int max = 100
*/
Comments