C and C ++ mixed programming (_ cplusplus)

Source: Internet
Author: User

First understanding
For example, if you have developed a dll library using C ++, in order to enable the C language to call your DLL output (export) function, you need to use extern "c" to force the compiler not to modify your

Function name.

Generally, you can see code similar to the following in the header file of C language:

# Ifdef _ cplusplus

Extern "C "{

# Endif

 

# Ifdef _ cplusplus

 }

# Endif

So what is the purpose of this writing? In fact, this is a syntax form used to enable CPP to interact with C interfaces. This method is used because of the difference between the two languages.

Exception. Since CPP supports polymorphism, that is, functions with the same function name can complete different functions, CPP usually uses parameters to differentiate which function is called. In

During compilation, the CPP compiler connects the parameter type and function name. After the program is compiled into the target file, the CPP compiler can directly follow the symbol name in the target file.

Connect multiple target files to a target file or executable file. However, in C language, because there is no concept of polymorphism at all, the C compiler will add

In addition to an underscore, nothing can be done (at least many compilers do this ). For this reason, when using CPP and C Mixed Programming, problems may occur. False

A function defined in a header file:

Int Foo (int A, int B );

The implementation of this function is located in a. c file. At the same time, this function is called in the. cpp file. When the CPP compiler compiles this function, it may

Change the name to _ fooii. Here, II indicates that the first and second parameters of the function are both integer. The C compiler may compile the function name into _ Foo. That is to say, in the CPP compiler

In the target file, the Foo () function is referenced by the _ fooii symbol. In the target file generated by the C compiler, the Foo () function is referred to by _ Foo. But when the connector works, it

However, no matter what language the upper layer uses, it only recognizes the symbols in the target file. Therefore, the connector will find that the Foo () function is called in. cpp, but it cannot be found in other target files.

To the _ fooii symbol, an error occurs during the connection. Extern "C" {} is used to solve this problem. This document describes the problem as an example.

Assume that there are three files as follows:

 

# Ifndef _ test_extern_c_h __

# DEFINE _ test_extern_c_h __

# Ifdef _ cplusplus

Extern "C "{

# Endif

 

Extern int thisistest (int A, int B );

# Ifdef _ cplusplus

 }

# Endif

# Endif

This header file defines only one function, thisistest (). This function is defined as an external function and can be included in other program files. Suppose thisistest () function

The implementation is located in the test_extern_c.c file:

 

# Include "test_extern_c.h"

Int thisistest (int A, int B)

{

 Return (A + B );

}

As you can see, the implementation of the thisistest () function is very simple, that is, the result of adding the two parameters is returned. Now, assume that you want to call the thisistest () function from CPP:

 

# Include "test_extern_c.h"

# Include <stdio. h>

# Include <stdlib. h>

Class Foo {

 Public:

 Int bar (int A, int B)

   {

       Printf ("result = % I \ n", thisistest (a, B ));

   }

};

Int main (INT argc, char ** argv)

{

 Int A = atoi (argv [1]);

 Int B = atoi (argv [2]);

 Foo * Foo = new Foo ();

 Foo-> bar (A, B );

 Return (0 );

}

In this CPP source file, a simple class foo is defined, and the thisistest () function is called in its member function bar. Next, let's take a look at how to compile test_extern_c.c using gcc.

And use g ++ to compile main. cpp and connect to test_extern_c.o:

[Cyc @ Cyc SRC] $ gcc-C test_extern_c.c

[Cyc @ Cyc SRC] $ g ++ main. cpp test_extern_c.o

[Cyc @ Cyc SRC] $./A. out 45         

Result = 9

**************************************** **************************************** ******

In Windows, unlike g ++ in Linux, you can enter the. out parameter 1 parameter 2 ....

In VC, how should I perform this operation?

Int main (INT argc, char * argv [])
If you run the script directly, the local argcis 1, and the physical address of the .exe file is 0, you can set the breakpoint to follow up.

If you want multiple parameters, run the following command: for example:

Int main (INT argc, char * argv [])
{
  
  For (INTI = 0; I <argc; I ++)
  
  Cout <argv [I] <endl1;

}
Assume that the file you generated is 123.exe under c;

Enter c: \ 123 aaaa bbbb cccc in the command line.

Then the program will eventually output c: \ 123.exe
                  Aaaa
                  Bbbb
                  CCCC
These four strings <these are all passed to the main function through the command line>
If you click run directly after compilation, only c: \ 123.exe will be output.

For example, this notepad 123.txt file will be opened after it is run.
They are all rational.

I have read the source code of multiple Linux software and found that many projects have main (INT argc, char * argv []) or (intargc, char ** argv ),

Find the following description on the Internet.

When I first came into contact with these two variables, I didn't know what they are used for. I think many people are just like me. I was confused when I saw these two variables.

In fact: int main (INT argc, char * argv []) is a standard writing method in UNIX and Linux, while intmain () is only a default usage in UNIX and Linux ..

What is the purpose of argc and argv? The following example test_argc.c will understand their usage:

# Include <unistd. h>
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
If (argc = 1 | argc> 2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.