在c++函数参数列表中有字符串类型时,我可以使用C程序函数调用c++函数吗?

时间:2021-06-11 16:03:34

My C program application needs to call C++ function . But there is string type in C++ function . For example ,I need to write a function fooC() like this:

我的C程序应用程序需要调用c++函数。但是c++函数中有字符串类型。例如,我需要编写这样的函数fooC():

//main.c:

void fooC()
{
   char* str = "hello";
   fooCPP(str);
}


//foo.cpp

void fooCPP(String& str)
{
  ......
}

How to write code correctly?

如何正确地编写代码?

update

更新

//hello.cpp 
#include <iostream>
#include <string>
#include "hello.h"
using namespace std;

void fooCpp(char const* cstr){
    std::string str(cstr);
    cout << str <<endl;
}

//hello.h
#ifdef __cplusplus
extern "C"{
#endif
void fooCpp(char const* str);

#ifdef __cplusplus
}
#endif

//main.c
#include "hello.h"

int main()
{
    char* str = "test"  ;
    fooCpp(str);
    return 0;
}

compile:

编译:

g++ -c hello.cpp hello.h

g++ - c你好。cpp hello.h

gcc hello.o main.c -g -o main

gcc你好。o主要。c - g - o主要

error :

错误:

hello.o: In function __static_initialization_and_destruction_0(int, int)': hello.cpp:(.text+0x23): undefined reference tostd::ios_base::Init::Init()' hello.o: In function __tcf_0': hello.cpp:(.text+0x6c): undefined reference tostd::ios_base::Init::~Init()' hello.o: In function fooCpp': hello.cpp:(.text+0x80): undefined reference tostd::allocator::allocator()' hello.cpp:(.text+0x99): undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' hello.cpp:(.text+0xa4): undefined reference tostd::allocator::~allocator()'.............................. ....................................

你好。在函数__static_initialization_and_destruction_0(int, int)中:hello.cpp:(.text+0x23):未定义的引用tostd::ios_base::Init()“hello”。o:在函数__tcf_0中:hello.cpp:(.text+0x6c):未定义引用tostd:::ios_base:::Init:: Init ~ ()' hello。o:在函数fooCpp:hello.cpp:(。text + 0 x80):未定义的参考tostd::分配器::分配器()“hello.cpp:(。text + 0 x99):未定义的引用std::basic_string < char,std::char_traits < char >、std::分配器< char > >::basic_string(const char *,std::分配器< char >常量)的hello.cpp:(。text + 0 xa4):未定义的参考tostd::分配器::~分配器()“.............................. ....................................

1 个解决方案

#1


13  

Nope. You need to write a wrapper in C++:

不。您需要用c++编写一个包装器:

//foo.cpp

void fooCPP(std::string& str)
{
  ......
}

extern "C" void fooWrap(char const * cstr)
{
    std::string str(cstr);
    fooCPP(str);
}

And call it from C:

从C调用:

/*main.c:*/
extern void fooWrap(char const * cstr); /*No 'extern "C"' here, this concept doesn't exist in C*/

void fooC()
{
    char const* str = "hello";
    fooWrap(str);
}

#1


13  

Nope. You need to write a wrapper in C++:

不。您需要用c++编写一个包装器:

//foo.cpp

void fooCPP(std::string& str)
{
  ......
}

extern "C" void fooWrap(char const * cstr)
{
    std::string str(cstr);
    fooCPP(str);
}

And call it from C:

从C调用:

/*main.c:*/
extern void fooWrap(char const * cstr); /*No 'extern "C"' here, this concept doesn't exist in C*/

void fooC()
{
    char const* str = "hello";
    fooWrap(str);
}