The C ++ programming language is widely used and has flexible application methods. It supports multiple programming styles and helps us solve many problems easily. For example, the specific operation method for C ++ to call the C link library introduced today is a classic example, which allows you to fully understand the concept.
- Detailed description of how C ++ generates random numbers
- Practical application skills of C ++ linked list operations
- A Brief Introduction to the C ++ Operating Mechanism
- Basic concepts of C ++ cyclic statements
- Application Methods for C ++ class members
C ++ calls the C link library, but it actually calls C ++ relative to C. Because C ++ is inherently backward compatible with C's personal opinions ).
However, due to the different design of the compiler, some problems may occur, such as: In the middle part, there is an undefined reference to 'helloc () ', helloC () is my function in the C-Linked Library)
Simply put, the reason is that the function name may change to _ helloC () after the unprocessed C code is compiled, but the function name after the C ++ compilation is not like this, so it is not correct.
You need to add extern "C" to the include c file"
In the exercise, three files are written: c. h c. c cpp. The first two files are compiled as C-linked libraries, and cpp. cpp uses C. It verifies that C ++ calls the C link library to access the global variables of C.
C. h:
- #include "stdio.h"
- void helloC();
- int abc = 1;
C. c:
- #include <stdio.h>
- #include "c.h"
- void helloC(){
- printf("I am Kenko");
- }
Cpp. cpp: (focus on include)
- #include <iostream>
- using namespace std;
- extern "C" {
- #include "c.h"
- }
- int main(){
- helloC();
- cout<<abc<<endl;
- return 0;
- }
This section describes how C ++ calls the C-Linked Library.