c/c++單例模式類的混合編譯案例詳解
C/C++混合編譯
難點(diǎn):c++支持重載,因此g++編譯后的函數(shù)名有額外信息,在gcc編譯的c文件中無(wú)法識(shí)別符號(hào),導(dǎo)致鏈接失敗。
解決方案:
- extern “C” { }
- 中間層調(diào)用 extern “C”
? 對(duì)c++文件編譯時(shí)使用extern “C“ { },讓編譯器安裝c語(yǔ)言的規(guī)則對(duì)其中的內(nèi)容進(jìn)行編譯,主要解決c++中重載函數(shù)名導(dǎo)致符號(hào)不識(shí)別的問(wèn)題。
? 同時(shí)配合ifdef __cplusplus和endif實(shí)現(xiàn)文件(主要是頭文件)被gcc和g++編譯時(shí)能夠自動(dòng)匹配當(dāng)前編譯器的語(yǔ)言。另一方面也是因?yàn)閏語(yǔ)言不支持extern “C”關(guān)鍵字。
中間層調(diào)用
? 由于c語(yǔ)言中沒(méi)有類的概念,因此對(duì)于有類的cpp文件與c文件混合編譯時(shí),提供一個(gè)中間層提供類的操作接口,在c文件中調(diào)用接口實(shí)現(xiàn)間接操作類對(duì)象。
log案例
背景:main.c中需要調(diào)用logClass.cpp文件中的logClass類的相關(guān)成員函數(shù),并且該類是一個(gè)單例模式。
解決方案:
文件目錄
│main.c
├─include
│ interFace.h
│ logClass.h
│
└─src
interFace.cpp
logClass.cpp
源代碼
main.c
#include "interFace.h"
#include <stdint.h>
#include <stdio.h>
int main()
{
set_log_count(10);
uint32_t count = get_log_count();
printf("The conut is %d\n", count);
}
logClass.h
#ifndef LOG_CLASS_H
#define LOG_CLASS_H
#include <stdint.h>
#include <stdio.h>
#define FCA_BOOL uint16_t
#define FCA_TRUE 1
#define FCA_FALSE 0
class logClass
{
public:
static logClass *getInstance()
{
static logClass m_plogClass;
return &m_plogClass;
}
FCA_BOOL setLogCount(uint32_t num);
uint32_t getLogCount();
private:
logClass();
logClass(const logClass &) = delete;
logClass &operator=(const logClass &) = delete;
~logClass();
uint32_t m_logCount;
static logClass* m_plogClass;
};
#endiflogClass.cpp
#include "logClass.h"
logClass::logClass(/* args */)
{
printf("log class construct!!!!!\n");
}
logClass::~logClass()
{
printf("log class destruct!!\n");
}
FCA_BOOL logClass::setLogCount(uint32_t num)
{
m_logCount = num;
return FCA_TRUE;
}
uint32_t logClass::getLogCount()
{
return m_logCount;
}interFace.cpp
#include "interFace.h"
#include "logClass.h"
logClass* log = logClass::getInstance();
FCA_BOOL set_log_count(uint32_t num)
{
FCA_BOOL ret = log->setLogCount(num);
return ret;
}
uint32_t get_log_count()
{
return log->getLogCount();
}
interFace.h
#ifndef INTERFACE_H
#define INTERFACE_H
#include <stdint.h>
#define FCA_BOOL uint16_t
#define FCA_TRUE 1
#define FCA_FALSE 0
#ifdef __cplusplus
extern "C"
{
#endif
FCA_BOOL set_log_count(uint32_t num);
uint32_t get_log_count();
#ifdef __cplusplus
}
#endif
#endif
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(MYLOGTEST CXX C)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") #設(shè)置c++的編譯選項(xiàng)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") #設(shè)置c的編譯選項(xiàng)
include_directories(include)
add_executable(mylogtest main.c src/logClass.cpp src/interFace.cpp)
到此這篇關(guān)于c/c++單例模式類的混合編譯的文章就介紹到這了,更多相關(guān)c++混合編譯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言編寫Linux守護(hù)進(jìn)程實(shí)例
這篇文章主要介紹了C語(yǔ)言編寫Linux守護(hù)進(jìn)程實(shí)例,本文講解了守護(hù)進(jìn)程及其特性、守護(hù)進(jìn)程的編程要點(diǎn)、守護(hù)進(jìn)程代碼實(shí)例等內(nèi)容,需要的朋友可以參考下2015-02-02
C++實(shí)現(xiàn)圖書管理系統(tǒng)源碼
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C語(yǔ)言演示對(duì)歸并排序算法的優(yōu)化實(shí)現(xiàn)
這篇文章主要介紹了C語(yǔ)言演示對(duì)歸并排序算法的優(yōu)化實(shí)現(xiàn),歸并排序的最差時(shí)間復(fù)雜度為(n\log n),最優(yōu)時(shí)間復(fù)雜為(n),存在可以改進(jìn)的空間,需要的朋友可以參考下2016-05-05
斐波那契數(shù)列 優(yōu)化矩陣求法實(shí)例
斐波那契數(shù)列 優(yōu)化矩陣求法實(shí)例,需要的朋友可以參考一下2013-03-03

