gcc与g++的编译链接的示例详解
一、编译方式的示例详解
1. 编译C代码
代码如下:main.c
/*!
******************************************************************************
* \File
* main.c
* \Brief
* C codes
* \Author
* Hank
******************************************************************************
*/
#include <stdio.h>
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
ret = add(a,b);
printf(" %d + %d = %d\n",a,b,ret);
return;
}
int add(int a, int b)
{
return (a + b);
}
1.1 用gcc编译:
$ gcc -Wall -g main.c -o gcc_compile_c
main.c: In function 'main':
main.c:20: warning: implicit declaration of function 'add'
main.c:23: warning: 'return' with no value, in function returning non-void
运行:
$ ./gcc_compile_c
4 + 3 = 7
1.2. 用g++编译
代码如上所示
$ g++ -Wall -g main.c -o g++_compile_c
main.c: In function 'int main(int, char**)':
main.c:20: error: 'add' was not declared in this scope
main.c:23: error: return-statement with no value, in function returning 'int'
改成如下:
#include <stdio.h>
int add(int a, int b)
{
return (a + b);
}
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
ret = add(a,b);
printf(" %d + %d = %d\n",a,b,ret);
return 0;
}
编译运行:
$ g++ -Wall -g main.c -o g++_compile_c
$ ./g++_compile_c
4 + 3 = 7
可见,C++的规则检查强于C;
2. 编译C++代码
代码如下: main.cpp
#include <iostream>
/*!
******************************************************************************
* \File
* main.cpp
* \Brief
* C++ source code
* \Author
* Hank
******************************************************************************
*/
#include <iostream>
using namespace std;
class Arithmetic
{
private:
int m_iVarA;
int m_iVarB;
public:
int add();
int sub();
int mul();
int div();
int mod();
public:
Arithmetic();
Arithmetic(int, int);
~Arithmetic();
};
int main(int argc, char* argv[])
{
int a = 4, b = 3;
int ret = 0;
Arithmetic arith(a, b);
ret = arith.add();
cout<<a<<" + "<<b<<" = "<<ret<<endl;
return 0;
}
Arithmetic::Arithmetic(int a, int b):m_iVarA(a),m_iVarB(b)
{}
Arithmetic::~Arithmetic()
{
m_iVarA = 0;
m_iVarB = 0;
}
int Arithmetic::add()
{
return (m_iVarA + m_iVarB);
}
2.1 用gcc编译
$ gcc -Wall -g -lstdc++ main.cpp -o gcc_compile_c++
$ ./gcc_compile_c
4 + 3 = 7
2.2 用g++编译
$ g++ -Wall -g main.cpp -o g++_compile_c++
$ ./g++_compile_++
4 + 3 = 7
3. 编译 C++代码中含有C语言的代码
代码main.cpp
/*!
******************************************************************************
* \File
* main.cpp
* \Brief
* C++ source code
* \Author
* Hank
******************************************************************************
*/
#include <iostream>
using namespace std;
class Arithmetic
{
private:
int m_iVarA;
int m_iVarB;
public:
int add();
int sub();
int mul();
int div();
int mod();
public:
Arithmetic();
Arithmetic(int, int);
~Arithmetic();
};
extern "C" int add(int, int);
int main(int argc, char* argv[])
{
int a = 4, b = 3;
int ret = 0;
Arithmetic arith(a, b);
ret = add(a, b);
cout<<a<<" + "<<b<<" = "<<ret<<endl;
return 0;
}
int add(int a, int b)
{
return (a + b);
}
Arithmetic::Arithmetic(int a, int b):m_iVarA(a),m_iVarB(b)
{}
Arithmetic::~Arithmetic()
{
m_iVarA = 0;
m_iVarB = 0;
}
int Arithmetic::add()
{
return (m_iVarA + m_iVarB);
}
3.1 用gcc编译
$ gcc -Wall -g -lstdc++ main.cpp -o gcc_compile_cINc++
$ ./gcc_compile_cINc++
4 + 3 = 7
3.2 用g++编译
$ gcc -Wall -g main.cpp -o g++_compile_cINc++
$ ./g++_compile_cINc++
4 + 3 = 7
二、各种库的编译链接方式示例详解
1. C语言代码链接调用C语言库
库代码文件:
/*!
******************************************************************************
* \File
* arith.h
******************************************************************************
*/
#ifndef __ARITH_H__
#define __ARITH_H__
int add(int a, int b);
#endif
/*!
******************************************************************************
* \File
* arith.c
******************************************************************************
*/
#include "arith.h"
int add(int a, int b)
{
return (a + b);
}
调用库的代码:
/*!
******************************************************************************
* \File
* main.c
* \Brief
* C codes
* \Author
* Hank
******************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "arith.h"
int main(int argc, char* argv[])
{
int a = 4;
int b = 3;
int ret = 0;
// 加载动态库
void *p_Handler = dlopen("./arith.so", RTLD_NOW);
if (!p_Handler)
{
printf("%s\n",dlerror());
exit(1);
}
// 引入接口函数
char *error;
typedef int (*arith_add)(int, int);
arith_add pf_add_interface;
pf_add_interface = (arith_add)dlsym(p_Handler, "add");
error = dlerror();
if (error)
{
printf("%s\n", error);
exit(1);
}
// 调用
ret = pf_add_interface(a,b);
printf(" %d + %d = %d\n",a,b,ret);
dlclose(p_Handler); // 关闭句柄
return 0;
}
1.1 gcc实现
生成库:
$ gcc -Wall -g -fPIC -o arith.so -shared arith.c
生成可执行文件:
$ gcc -Wall -g -rdynamic -ldl main.c -o compile_cLIBc
$ ./compile_cLIBc
3 + 4 = 7
1.2 g++实现
生成库:
$ g++ -Wall -g -fPIC -o arith.so -shared arith.c
生成可执行文件:
$ g++ -Wall -g -rdynamic -ldl main.c -o compile_cLIBc
$ ./compile_cLIBc
3 + 4 = 7
2. C++代码链接调用C++库
请参见文章《linux下C++动态链接C++库》详解
3. C++代码链接调用 C语言库
请参见文章《linux下C++动态链接C语言库》详解