我正在尝试按照本教程学习如何使用 C++ 和 ODB:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2
我创建了一个 Person.hxx 文件,其中将 Person 类声明为持久性,然后我有三个文件 Person-odb: .cxx, .hxx, .ixx
现在我应该编译 Person-odb.cxx
g++ -I/usr/lib/odb/i686-linux-gnu/include Person-odb.cxx
但它以:
fatal error: odb/pgsql/version.hxx: No such file or directory. compilation terminated.
我看到有一个文件 version.hxx 但没有 odb/pgsql 目录...怎么了?
这是 Person.hxx,我在其中定义了持久类 Person:
#ifndef PERSON_HXX
#define PERSON_HXX
#include <string>
#include <odb/core.hxx>
using namespace std;
#pragma db object
class Person {
private:
Person() {
}
friend class odb::access;
#pragma db id auto
unsigned long id_;
std::string email_;
std::string first_;
std::string last_;
unsigned short age_;
public:
Person(const std::string& first, const std::string& last,
unsigned short age);
/* getters */
const std::string& first() const;
const std::string& last() const;
unsigned short age() const;
const std::string& email() const;
/* setters */
void setAge(unsigned short);
void setFirst(const std::string&);
void setLast(const std::string&);
void setEmail(const std::string&);
};
#endif
然后我必须用 odb 编译器编译 Person.hxx:
odb -d mysql --generate-query --generate-schema Person.hxx
我得到 4 个文件 Person.odb.hxx, .cxx, .sql, .ixx 这是 driver.cxx 我有持久化对象的主程序:
#include <memory>
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include "Person.hxx"
#include "Person-odb.hxx"
using namespace std;
using namespace odb;
int main(int argc, char* argv[]) {
try {
auto_ptr<database> db (new odb::mysql::database (argc, argv));
unsigned long marcoID, loryID, lucaID;
/*Create some persistent Person objects */
Person marco ("Marco", "Di Nicola", 26);
Person luca ("Luca", "La Sala", 22);
Person lory ("Lorenzo", "Vinci", 24);
transaction t (db->begin());
marcoID = db->persist(marco);
lucaID = db->persist(luca);
loryID = db->persist(lory);
t.commit();
} catch (const odb::exception& e) {
cerr << e.what() << endl;
return 1;
}
}
这是文件 Person-odb.hxx
// This file was generated by ODB, object-relational mapping (ORM)
// compiler for C++.
//
#ifndef PERSON_ODB_HXX
#define PERSON_ODB_HXX
#include <odb/version.hxx>
#if (ODB_VERSION != 20200UL)
#error ODB runtime version mismatch
#endif
#include <odb/pre.hxx>
#include "Person.hxx"
#include <memory>
#include <cstddef>
#include <odb/core.hxx>
#include <odb/traits.hxx>
#include <odb/callback.hxx>
#include <odb/wrapper-traits.hxx>
#include <odb/pointer-traits.hxx>
#include <odb/container-traits.hxx>
#include <odb/no-op-cache-traits.hxx>
#include <odb/result.hxx>
#include <odb/simple-object-result.hxx>
#include <odb/details/unused.hxx>
#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Person
template <>
struct class_traits< ::Person >
{
static const class_kind kind = class_object;
};
template <>
class access::object_traits< ::Person >
{
...
#include "Person-odb.ixx"
#include <odb/post.hxx>
#endif // PERSON_ODB_HXX
当我执行时,一切似乎都正常:
c++ -c Person-odb.cxx
c++ -c driver.cxx
但最终当我必须将所有内容链接在一起时:
c++ -o driver driver.o Person-odb.o -lodb-mysql -lodb
我得到:
“对‘Person::Person(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, unsigned short)’的未定义引用”