In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the method of C calling C++ code". In daily operation, I believe many people have doubts about the method of C calling C++ code. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of C calling C++ code?" Next, please follow the editor to study!
Sometimes the C++ class is needed in the C program, but the C language cannot call the class directly, so it is necessary to encapsulate the C++ class with C interface before calling
The encapsulated C++ code can be compiled into a library file for C language to call.
It is important to note that the encapsulated C++ code base file is compiled with glossy +, so when called in C, you need to add the extern "C" {} keyword.
When compiling c code, add-lstdc++
The following code is an example of c code using C++ 's map container:
/ / test.cpp encapsulates C++ code # include # include # include "test.h" using namespace std;static map m _ testMap _ val; _ void pushVal (int key, int val) {m _ testmap [key] = val;} int getVal (int key) {map::iterator iter = m_testMap.find (key); if (iter! = m_testMap.end ()) {return iter- > second;} return-1 } / / header file test.h#ifndef _ TEST_H_#define _ TEST_H_#ifdef _ cplusplusextern "C" {# endifvoid pushVal (int key, int val); int getVal (int key); # ifdef _ cplusplus} # endif#endif
Main function, which calls the encapsulated C++ API:
/ / main.c#include # include "test.h" int main () {printf ("test\ n"); for (int I = 0; I < 10; iTunes +) {printf ("push key:% d, val:% d\ n", I, iTunes 10); pushVal (I, iTunes 10);} int val = 0; for (int I = 0; I < 10; iTunes +) {val = getVal (I) Printf ("get key:% d, val:% d\ n", iMagery Val);} return 0;}
When compiling, for simplicity, I didn't compile it into a library file here, but directly compiled it with reference .o:
Makefile:
All: main.c +-Wall-c test.cpp-o test.o gcc-Wall-c main.c-o main.o gcc-o test-lstdc++clean: rm test * .o
The result of the compilation is as follows:
Makeg++-Wall-c test.cpp-o test.ogcc-Wall-c main.c-o main.ogcc-Wall test.o main.o-o test-lstdc++
Run:
. / test
Test
Push key: 0, val: 0
Push key: 1, val: 10
Push key: 2, val: 20
Push key: 3, val: 30
Push key: 4, val: 40
Push key: 5, val: 50
Push key: 6, val: 60
Push key: 7, val: 70
Push key: 8, val: 80
Push key: 9, val: 90
Get key: 0, val: 0
Get key: 1, val: 10
Get key: 2, val: 20
Get key: 3, val: 30
Get key: 4, val: 40
Get key: 5, val: 50
Get key: 6, val: 60
Get key: 7, val: 70
Get key: 8, val: 80
Get key: 9, val: 90
At this point, the study of "what is the method of C calling C++ code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 292
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.