1

After following some simple tutorial, I want to convert my cpp program to a dll file, however, it return several errors, like

C2526'split':c linkage function cannot return c++ class 'std::vector<std::string,std::allocator<std::string>>'

C2371'split':redefinition;different basic types
C2491 'split':definition of dllimport function not allowed
C2065 'split':undeclared identifier

in pch.h

#ifndef PCH_H
#define PCH_H

#include "framework.h"

#endif //PCH_H

#ifdef IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport) //I follow tutor, but I doubt it cannot works if you use cpp when define extern "C"
#endif

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
IMPORT_DLL std::vector<std::string> split(std::string s, std::string delimiter);
IMPORT_DLL std::string removespaces(std::string str);
IMPORT_DLL std::string findevent(std::string str);

in pch.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

std::vector<std::string> split(std::string s, std::string delimiter) 
{}
std::string removespaces(std::string str)
{}
std::string findevent(std::string str)
{}

I am sorry, I am a novice in cpp and dll,so any suggestion is helpful! Need I rewrite all program to c, not cpp?

12
  • 2
    Unless you can guarantee that the application is built with the same compiler, same compiler options, same runtime library options, etc. as the DLL, then std::string, std::vector, and basically any type that can have differing internals or use heap memory are not recommended to be used as parameters or return values in the exported/imported functions. Instead, regular C (or Windows types such as LONG, BOOL, etc. and pointers to those types) should be used. Commented Jul 26, 2022 at 8:38
  • 1
    As the error message says, you declare the function as C but return a C++ class instance. This cannot work. Commented Jul 26, 2022 at 9:04
  • @thebusybee so how can I declare a c++ function? Commented Jul 26, 2022 at 9:17
  • @PaulMcKenzie could you please give me some suggestion in how to write a c++ declaration? Commented Jul 26, 2022 at 9:19
  • @PaulMcKenzie besides, can I use c++ methods, for example string, vector, in my function, while return a c class, like, char, bool, etc? Commented Jul 26, 2022 at 9:34

1 Answer 1

1

About C calling C++ DLL, you need to pay attention to the following points:

1.The C++ function interface for C calls cannot contain C++-specific things.

2.When compiling a dll called by c code, extern "C" should be added before the function declaration in the header file to tell the compiler to process the function name according to the c specification.

3.After the compilation is completed, the header file provided for c cannot contain extern "C", which can be solved by using the macro switch, or by rewriting a header file.

Sign up to request clarification or add additional context in comments.

2 Comments

if a cpp function interface cannot contain cpp specific things, what can it contain? could you please give some examples, like class or something?
@4daJKong Hi, for example, you could use int char, something that exists in C language, but you could not use string, it only exists in C++. On the other hand, in C language, there is no class, only struct, about struct you could read this document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.