[odb-users] Questions about export view class as DLL

Music samal zxbzswxr at gmail.com
Sat Nov 10 21:20:37 EST 2018


I want to use view structs on occasions.
So I write some sample code to test it.
It works well when I build it as EXE.
But when I export it as dll.
and use it in another project.
VS post link errors LNK2001

let me show the code and error .
my test.hxx
//////    test.hxx
///////
//////
/////   odb compile command
   /*
       odb -I D:\ODB\odbfiles
       -I E:\boost_1_67_0
       -d pgsql
      --profile boost
       --export-symbol  ODB_MODULE_EXPORT
      --generate-query
     --generate-schema
      --schema-format sql
        test.hxx
*/
#pragma once
#ifdef BUILD_EXPORT
#define ODB_MODULE_EXPORT __declspec(dllexport)
#elif defined USE_DLL
#define ODB_MODULE_EXPORT __declspec(dllimport)
#else
#define ODB_MODULE_EXPORT
#endif


#include <string>
//  use boost::uuids::uuid as  object Id
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>

#include <odb/tr1/memory.hxx>
#include <odb/tr1/lazy-ptr.hxx>

using boost::uuids::uuid;
using std::tr1::shared_ptr;
using std::tr1::weak_ptr;
using odb::tr1::lazy_weak_ptr;
using odb::tr1::lazy_shared_ptr;
using namespace odb::core;
using namespace std;

class SomeB;
#pragma db object pointer(std::tr1::shared_ptr)
class ODB_MODULE_EXPORT SomeA
{
public:
SomeA() {}
friend class odb::access;
#pragma db id
uuid m_Id;

string m_strNodeId;
#pragma db inverse(m_SupA)
vector<lazy_weak_ptr<SomeB> >  m_vBs;

};
#pragma db object pointer(std::tr1::shared_ptr)
class ODB_MODULE_EXPORT SomeB
{
public:
SomeB() {}
friend class odb::access;
#pragma db id
uuid m_Id;
string m_strNodeId;
#pragma db on_delete(cascade)
lazy_shared_ptr<SomeA> m_SupA;
};

#pragma db view object(SomeB) pointer(std::tr1::shared_ptr)
struct ODB_MODULE_EXPORT B_NodeId
{
string m_strNodeId;
};

///////
///////   here is main.cpp  in another project .
//////   this main.cpp works well when it is builded with
////    test.hxx , test-odb.hxx,test-odb.cxx ......
/////     as an exe
/////  so I just remove main.cpp from project and build remain files
/////  and export them as DLL.
/////
////////  I copy the main.cpp to another project and import previous DLL
///////    LINK error occured
//////    error messages is listed at the bottom

#include "database.hxx"
#include <windows.h>
#include "test.hxx"
#include "test-odb.hxx"
#include <boost/uuid/random_generator.hpp>
using boost::uuids::random_generator;
using namespace odb::core;
// using namespace boost::uuids;

int main()
{

shared_ptr<database> db(new odb::pgsql::database("postgres", "123456",
"postgres", "localhost", 5432));
try
{
transaction t(db->begin());
random_generator rgen;
shared_ptr<SomeA>  SomeA1(new SomeA);
SomeA1->m_Id = rgen();
SomeA1->m_strNodeId = "SomeA1";
uuid SomeA1Id = db->persist(SomeA1);
vector<uuid>  vSomeBs;
for (unsigned int i = 0; i < 5; i++)
{
shared_ptr<SomeB> tempB(new SomeB);
tempB->m_Id = rgen();
tempB->m_strNodeId = "SomeB_" + to_string(i);
tempB->m_SupA = SomeA1;
vSomeBs.emplace_back(db->persist(tempB));
}
t.commit();
t.reset(db->begin());
result<B_NodeId>
resB_NodeIds(db->query<B_NodeId>(odb::query<B_NodeId>::SupA == SomeA1Id));

for (auto iB : resB_NodeIds)
{
cout << "B_NodeId : " << iB.m_strNodeId << endl;

}

db->erase<SomeA>(SomeA1Id);
t.commit();


}
catch (const odb::exception& odberror)
{
cerr << odberror.what() << endl;
}
catch (...)
{


}
system("pause");
return 0;
}
////////
/////// this is link error tips
///////
LNK2001 unsolved external symbol "public: static struct
odb::pgsql::query_column<struct boost::uuids::uuid,14> const
odb::pointer_query_columns<class SomeB,2,class
odb::access::object_traits_impl<class SomeB,2> >::SupA" (?SupA@
?$pointer_query_columns at VSomeB@@$01V?$object_traits_impl at VSomeB@@$01 at access
@odb@@@odb@@2U?$query_column at Uuuid@uuids at boost@@$0O@@pgsql at 2@B)
structviewDllTest E:\structviewDllTest\main.obj 1

odb  version: 2.4.0 release
OS  : win10
Compiler: VS2017
help!!


More information about the odb-users mailing list