網站首頁 編程語言 正文
C/C++混合編譯
難點:c++支持重載,因此g++編譯后的函數名有額外信息,在gcc編譯的c文件中無法識別符號,導致鏈接失敗。
解決方案:
- extern “C” { }
- 中間層調用 extern “C”
? 對c++文件編譯時使用extern “C“ { },讓編譯器安裝c語言的規則對其中的內容進行編譯,主要解決c++中重載函數名導致符號不識別的問題。
? 同時配合ifdef __cplusplus
和endif
實現文件(主要是頭文件)被gcc和g++編譯時能夠自動匹配當前編譯器的語言。另一方面也是因為c語言不支持extern “C”關鍵字。
中間層調用
? 由于c語言中沒有類的概念,因此對于有類的cpp文件與c文件混合編譯時,提供一個中間層提供類的操作接口,在c文件中調用接口實現間接操作類對象。
log案例
背景:main.c中需要調用logClass.cpp文件中的logClass類的相關成員函數,并且該類是一個單例模式。
解決方案:
文件目錄
│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; }; #endif
logClass.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") #設置c++的編譯選項 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") #設置c的編譯選項 include_directories(include) add_executable(mylogtest main.c src/logClass.cpp src/interFace.cpp)
原文鏈接:https://blog.csdn.net/Fat_Markov/article/details/127188948
相關推薦
- 2022-07-09 Tensorflow中使用cpu和gpu有什么區別_python
- 2022-07-12 解決錯誤:org.apache.ibatis.binding.BindingException
- 2022-03-16 linux下FastDFS搭建圖片服務器_Linux
- 2023-08-01 頁面滾動時隱藏element-ui下拉框/時間彈框
- 2024-04-05 docker部署mongodb
- 2022-10-14 Sklearn中的二分類模型可以進行多分類的原理
- 2022-05-27 C++實現數獨快速求解_C 語言
- 2022-07-06 python數據挖掘Apriori算法實現關聯分析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支