c ++ link indicator extern "C"


1. c ++ function using c

c c ++ standard library standard library, such as

cstdio   
/** @file include/cstdio
 *  This is a Standard C++ Library file.  You should @c #include this file
 *  in your programs, rather than any of the "*.h" implementation files.
 *
 *  This is the C++ version of the Standard C Library header @c stdio.h,
 *  and its contents are (mostly) the same as that header, but are all
 *  contained in the namespace @c std (except for names which are defined
 *  as macros in C).
 */


  extern "C" int
  (snprintf)(char * restrict, size_t, const char * restrict, ...);

 

A simple example again

-- math.h

#ifndef __MATH_H__
#define __MATH_H__

#ifdef __cplusplus
extern "C" {
#endif

int func(int,int);

#ifdef __cplusplus
}
#endif

#endif

 

-- math.c

#include "math.h"
int func(int a,int b)
{
    return a+b;
}

 

- compiled into a static library:
- gcc -c math.c
- Ar -r librmath.a * .o


-- math.cpp

#include " math.h " 
#include <the iostream>
 the using  namespace STD;
 int main () 
{ 
    int A = 2 , B = . 3 ;
     int c = FUNC (A, B);   // Direct call interfaces c 
    cout << c << endl; 
}
  

 


- compiled into an executable program:
- G ++ math.cpp -L./ -lrmath

-- ./a.out


If you do not add extern "C", the compiler Times link error:
. Math.cpp :( text + 0x21): Reference to undefined `FUNC (int, int) '
collect2: LD returned 1 Exit Status


2. Export function to c ++ c language

-- math.h

class sample {
    public:
        int func(int a, int b);
};

 

-- math.cpp

#include "math.h"
int sample::func(int a, int b)
{
    return a + b;
}

 

-- g++ -fpic -shared -g -o librmath.so math.cpp -I ./


Code-packaged write c

-- mymath.h

#ifdef __cplusplus
extern "C" {
#endif

int myfunc(int, int);

#ifdef __cplusplus
}
#endif

 

-- mymath.c

#include "mymath.h"

#include "math.h"

int myfunc(int a, int b)
{
    sample s;
    return s.func(a, b);
}

 

-- g++ -fpic -shared -g -o libmymath.so mymath.cpp -lrmath -I ./ -L ./


Test:
- main.c

#include <stdio.h>
#include "mymath.h"
int main()
{
    printf("%d\n", myfunc(1, 2));
    return 0;
}

 

-- gcc main.c -L./ -I ./ -lrmath -lmymath -Wl,-rpath=./

-- ./a.out

 

If you want to compile as a static library:

g++ -c math.cpp

-r our librmath.a math.o

g++ -c mymath.cpp 

-r our libmymath.a mymath.o

gcc main.c libmymath.a librmath.a -lstdc++

Two points

1. -lstdc ++

undefined reference to `__gxx_personality_v0'

because:

Note that programs using C++ object files must always be linked with g++, in order to supply the appropriate C++ libraries. Attempting to link a C++ object file with the C compiler gcc will cause "undefined reference" errors for C++ standard library functions

C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

Probably it means based on gcc extension to link the standard library, the above example does not detect c ++ gcc, so by default do not link c ++ foundation libraries.

 

2. main.c libmymath.a librmath.a order of these three can not move, because:

When the GNU linker sees a library, it discards all symbols that it doesn't need. In this case, your library appears before your .cpp file, so the library is being discarded before the .cpp file is compiled. 

So the recommended way dynamic library.

Guess you like

Origin www.cnblogs.com/tenyearboyue/p/12037824.html