我正在尝试学习如何使用C++和ODB,遵循本教程:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2
我已经创建了一个Person.hxx文件,其中有Person类的声明为persistent,然后我有三个文件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的Person.hxx:
#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)’的引用”
发布于 2013-06-26 21:44:00
您的问题似乎是您没有安装ODB。Installing ODB页面应该可以让您启动并运行。
您看到的是一个version.hxx文件,但它是用于ODB编译过程的输入文件,而不是用于包含在程序中。
如果您确实安装了它,请找到您系统上的位置,并将该文件夹添加到您的编译器包含路径。然后您的编译命令变为
g++ -I/usr/lib/odb/i686-linux-gnu/include -I/path/to/odb/install/include Person-odb.cxx
在您进行编辑之后,我认为问题在于您没有链接到Person
对象文件,而只是链接到Person-odb对象文件。
使用编译Person.cxx
g++ -c Person.cxx
并将链接命令更改为
g++ -o driver driver.o Person.o Person-odb.o -lodb-mysql -lodb
并且错误应该被修复。
请注意,我不了解ODB。我试图从纯C++的角度来解决这个问题。
https://stackoverflow.com/questions/17320867
复制相似问题