2015-04-02 46 views
0

我想编译odb附带的“Hello World”示例。我正在使用debian Linux。odb“Hello World”链接产生“未定义的参考”

我复制了person.hxx和driver.cxx文件

// person.hxx 
#ifndef person_hxx 
#define person_hxx 

#include <string> 
#include <odb/core.hxx> 

#pragma db object 
class person 
{ 
public: 
    person (const std::string& first, 
      const std::string& last, 
      unsigned short age); 

    const std::string& first() const; 
    const std::string& last() const; 

    unsigned short age() const; 
    void age (unsigned short); 

private: 
    person() {} 

    friend class odb::access; 

    #pragma db id auto 
    unsigned long id_; 

    std::string first_; 
    std::string last_; 
    unsigned short age_; 
}; 

#endif 


// 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::core; 

int main (int argc, char * argv[]) 
{ 
    try 
    { 
     auto_ptr<database> db (new odb::mysql::database (argc, argv)); 

     unsigned long john_id,jane_id, joe_id; 
     { 
     person john("John","Doe", 33); 
    person jane ("Jane","Doe", 32); 
     person joe("Joe","Dirt",30); 

    transaction t (db -> begin()); 

     john_id = db->persist(john); 
     jane_id = db->persist(jane); 
     joe_id = db->persist(joe); 

     t.commit(); 
     } 
    } 
    catch (const odb::exception& e) 
    { 
     cerr << e.what() <<endl; 
     return 1; 
    } 
} 

driverthe ODB编译器工作正常并制作人,ODB文件。

我编译他们

g++ -c deiver.cxx 
g++ -c person-odb.cxx 

和一切顺利。

的问题开始与链接阶段

g++ driver.o person-odb.o -lodb-mysql -lodb -o driver 

这reulted在

driver.cxx:(.text+0x14d): undefined reference to `person::person(std::string const&, std::string const&, unsigned short)' 
+3

你要么不编译也要链接'person.cpp',或者你不要定义构造函数person :: person'。链接器无法找到构造函数的目标代码。 – vsoftco 2015-04-02 18:49:23

+0

其中是person-odb.cxx?显示给我们。该链接器抱怨说它不包含构造函数的代码 – pm100 2015-04-02 18:52:58

+0

该示例没有person.cpp文件,只有一个名为“person-odb.cpp”的文件,由odb编译器 – 2015-04-02 18:53:12

回答

0

你需要添加实施构造。示例:

person (const std::string& first, 
     const std::string& last, 
     unsigned short age){} 
0

几年后,如果您复制并粘贴odb网站中提供的分步示例,仍然会遇到问题。 person.hxx文件中缺少该实现。

替换以下:

person (const std::string& first, 
      const std::string& last, 
      unsigned short age); 

与此:

person (const std::string& first, 
      const std::string& last, 
      unsigned short age) : 
     first_(first), 
     last_(last), 
     age_(age) 
    { 
    } 

此外,在driver.cxx您可以unique_ptr更换auto_ptr或编译这样:

g++ -g -std=c++98 -o driver driver.cxx person-odb.cxx -lodb-mysql -lodb 

你可以下载工作示例:https://www.codesynthesis.com/products/odb/download.xhtml