extern "C" 回顾
引入:在测试"extern "C" 与gcc, g++无关"时,使用到了extern "C"的概念,网上找篇文章回顾一下。
试验如下:
test.h:
extern "C" void CppPrintf(void);
test.cpp:
#include <iostream>
#include "test.h"
using namespace std;
void CppPrintf( void ) { cout << "Hellon"; }
main.cpp:
#include <stdlib.h>
#include <stdio.h>
#include "test.h"
int main( void ) { CppPrintf(); return 0; }
1. 先给 test.h 里的 void CppPrintf(void); 加上 extern "C",看用 gcc 和 g++ 命名有什么不同。
$ g++ -S test.cpp
$ less test.s
.globl CppPrintf <-- 注意此函数的命名
.type CppPrintf , @function
$ gcc -S test.cpp
$ less test.s
.globl CppPrintf <-- 注意此函数的命名
.type CppPrintf , @function
完全相同!
2. 去掉 test.h 里 void CppPrintf(void); 前面的 extern "C",看用 gcc 和 g++ 命名有什么不同。
$g++ -S test.cpp
$ less test.s
.globl _Z9CppPrintfv <-- 注意此函数的命名
.type _Z9CppPrintfv , @function
$ gcc -S test.cpp
$ less test.s
.globl _Z9CppPrintfv <-- 注意此函数的命名
.type _Z9CppPrintfv , @function
完全相同!
测试结论: 完全相同,可见 extern "C" 与采用 gcc 或 g++ 并无关系, 以上的试验还间接的印证了前面的说法:在编译阶段,g++ 是调用 gcc 的。
这里注意,使用extern "C" 必须加在c++文件中才起作用(包括通过include 头文件的方式间接包含)。
进入正题:
extern "C" 的含义使用参考如下链接:
http://blog.chinaunix.net/uid-21411227-id-1826909.html
关于extern "C"总结两条:
1、C++和C对产生的函数名字的处理是不一样的. 通过查看汇编文件(.s),可以看出区别。
2、为了在C++代码中调用用C写成的库文件,就需要用extern "C"来告诉编译器:这是一个用C写成的库文件,请用C的方式来链接它们。
extern "C" 回顾的更多相关文章
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- #ifdef __cplusplus extern c #endif 的作用
#ifdef __cplusplus // C++编译环境中才会定义__cplusplus (plus就是"+"的意思) extern "C" { // 告诉编 ...
- “#ifdef __cplusplus extern "C" { #endif”的定义-----C和C++的互相调用
"#ifdef __cplusplus extern "C" { #endif"的定义 看一些程序的时候老是有 "#ifdef __cplusplus ...
- extern "C"的用法
文章转自开源电子论坛:http://www.openedv.com/forum.php?mod=viewthread&tid=7986 看一些程序的时候老是有 “#ifdef __cplusp ...
- C++ 基础知识回顾总结
一.前言 为啥要写这篇博客?答:之前学习的C和C++相关的知识,早就被自己忘到一边去了.但是,随着音视频的学习的不断深入,和C/C++打交道的次数越来越多,看代码是没问题的,但是真到自己操刀去写一些代 ...
- Delphi XE7调用C++动态库出现乱码问题回顾
事情源于有个客户需使用我们C++的中间件动态库来跟设备连接通讯,但是传入以及传出的字符串指针格式都不正确(出现乱码或是被截断),估计是字符编码的问题导致.以下是解决问题的过程: 我们C++中间件动态库 ...
- #ifdef __cplusplus extern "C" { #endif”的定义的含义
看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...
- 备忘录:“#ifdef __cplusplus extern "C" { #endif”的定义
看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
随机推荐
- SpringBoot对比SpringMVC,SpringMVC 处理请求过程
(问较多:1.SpringBoot对比SpringMVC.2.SpringMVC 处理请求过程.问:springboot的理解 Spring,Spring MVC,Spring Boot 三者比较 S ...
- spring boot(九):Spring Boot中Redis的使用
Redis实战代码 1.引入 spring-boot-starter-redis <dependency> <groupId>org.springframework.boot& ...
- C# 窗体最大化(自适应任务栏位置)
this.FormBorderStyle = FormBorderStyle.None; this.Location=Screen.PrimaryScreen.WorkingArea.Location ...
- Hibernate非主键一对多关联。
Unit表 id,code User表 id,ucode ...@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="ucode" ...
- This page is about building Firefox Desktop
This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...
- Mysql高级查询 内连接和外连接详解
一.内连接(INNER JOIN) 1.等值连接 概述:指使用等号"="比较两个表的连接列的值,相当于两表执行笛卡尔后,取两表连结列值相等的记录. 语法: SELECT 列 FRO ...
- 顺序列表(栈/队列等)ADT[C++]
#include<iostream> using namespace std; //ADT template<class T> class SeqList{ public: / ...
- UBUNTU18.4环境下使用更好用的搜索引擎(无奈,只能起这样的标题)
初步安装 更新软件源 sudo apt-get update 安装pip (一个安装和管理 Python 包的工具) sudo apt-get install python-pip sudo apt- ...
- MacOs -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
1解决iterm远程登录主机报错 -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or ...
- python - socketserver 模块应用
server端: import socketserver import subprocess import json import struct class MyTCPHandler(socketse ...