c++ - Accessing a vector from a DLL -
am working in project have build library , use library in main function.the library .dll , has 7 headers , 5 source files.in header declared vector , implemented in 1 of source files.since have access vector in main program declared global in source file , extern in header file.now after build dll , linked main program cannot able access vector showing "unresolved external".i don't know mistake working load time linking getting error during run time linking.please welcomed.my code this
ntfs-struct.h ---- > library header _cdecl(dllexports) extern std::vector<std::string>files; ntfs-search.cpp ------ > library source file #include "ntfs-struct.h" vector<string>files; ---> global vector accessing in main program mft-list --- > main program #include "ntfs-struct.h" cout << "vector size" << files.size();
p.s since used dll link main program has run time linking set configuration in vs2010(platform working) delay dll.
use macro distinguish "build dll" case "build exe" one.
dll header:
// ntfs-struct.h #ifdef build_dll #define declspec_dll __declspec( dllexport ) #else #define declspec_dll __declspec( dllimport ) #endif declspec_dll extern std::vector<std::string> files;
dll implementation:
// ntfs-search.cpp #define build_dll #include "ntfs-struct.h" std::vector<std::string> files;
exe:
// mft-list.cpp #include "ntfs-struct.h" cout << "vector size" << files.size();
edit: how use exported function taking vector reference:
declspec_dll void updatevector( std::vector<std::string> & files );
if experience heap corruption, may because don't build exe , dll same , good options c runtime library. options must be : multi-threaded dll (/md)
or multi-threaded debug dll (/mdd)
set theses options in property->c++->code generation
edit2: how link
method one: use #pragma directive in dll header.
#pragma lib( comment, "mylib.lib" )
method two: use dependencies settings.
- make dll project dependency of exe in "project dependencies" solution
- set
yes
link library dependencies
inlinker->general
properties exe
method three: explicitly add library file (lib)
go linker->input
properties , add lib file additional dependencies
list, exe
Comments
Post a Comment