c++ - error LNK2019: unresolved external symbol _CreateFastString referenced in function _wmain -
i creating dll , providing entry point faststring
class using createfaststring
function:
faststring.h
:
#define export __declspec(dllexport) #define import __declspec(dllimport) class faststring { const int m_length; char* m_str; public: faststring(const char* str); ~faststring(); int length()const; int find(const char* str)const; }; extern "c" faststring* createfaststring(const char* str);
faststring.cpp
:
#include "stdafx.h" #include <string> #include "faststring.h" faststring* createfaststring(const char* str) { return new faststring(str); } faststring::faststring(const char* str): m_length(strlen(str)), m_str(new char[m_length+1]) {} faststring::~faststring() { delete[] m_str; } int faststring::length()const { return m_length; } int faststring::find(const char* str)const { return 1; }
main.cpp
:
#include "stdafx.h" #include <iostream> #include "faststring.h" int _tmain(int argc, _tchar* argv[]) { faststring* str = createfaststring("hello dll"); std::cout<<"the length "<<str->length()<<std::endl; return 0; }
during compilation, getting following errors:
teatapp.obj : error lnk2019: unresolved external symbol _createfaststring referenced in function _wmain d:\mfc\faststring\debug\teatapp.exe : fatal error lnk1120: 1 unresolved externals
in linker -> input -> additional dependencies
have provided path .lib
file.
can suggest what's going wrong. in advance.
config.h
:
#define my_dll_export __declspec(dllexport) #define my_dll_import __declspec(dllimport) // should defined when mydll built (more details below). #ifdef my_dll_exports #define my_dll_public my_dll_export #else #define my_dll_public my_dll_import #endif #define my_dll_private #ifdef __cplusplus #define my_dll_function extern "c" #else #define my_dll_function extern #endif
faststring.h
:
#include "config.h" class my_dll_public faststring { const int m_length; char* m_str; public: faststring(const char* str); ~faststring(); int length() const; int find(const char* str) const; }; my_dll_function faststring* my_dll_public createfaststring(const char* str);
faststring.cpp
:
#include "faststring.h" #include "stdafx.h" #include <string> faststring* createfaststring(const char* str) { return new faststring(str); } faststring::faststring(const char* str): m_length(strlen(str)), m_str(new char[m_length+1]) {} faststring::~faststring() { delete[] m_str; } int faststring::length()const { return m_length; } int faststring::find(const char* str)const { return 1; }
note: build faststring.cpp
my_dll_exports
defined, symbols marked my_dll_public
exported resulting dll. "exported", means visible dll consumers, such application. best practice supply definition during compilation. example, on msvc can done adding /dmy_dll_exports
compiler invocation.
main.cpp
:
#include "stdafx.h" #include "faststring.h" #include <iostream> int _tmain(int argc, _tchar* argv[]) { faststring* str = createfaststring("hello dll"); std::cout<<"the length "<<str->length()<<std::endl; return 0; }
note: main.cpp
application, dll consumer, therefore, should not define my_dll_exports
during compilation, symbols marked my_dll_public
imported consumed dll.
if dll has private functions or classes should not visible dll consumers, it's practice mark them my_dll_private
. finally, didn't see include guards in posted code, , omitted them in examples, beware should have them sure, don't forget add them of headers, if missing in real code.
Comments
Post a Comment