首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >odb "Hello World“链接导致”未定义的引用“

odb "Hello World“链接导致”未定义的引用“
EN

Stack Overflow用户
提问于 2015-04-03 02:46:25
回答 2查看 753关注 0票数 0

我正在尝试编译odb附带的"Hello World“示例。我使用的是debian Linux。

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

代码语言:javascript
复制
// 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;
    }
}

驱动程序odb编译器运行良好,并生成person-odb文件。

我用以下命令编译了它们

代码语言:javascript
复制
g++ -c deiver.cxx
g++ -c person-odb.cxx

一切都进行得很顺利。

问题从链接阶段开始

代码语言:javascript
复制
g++  driver.o  person-odb.o -lodb-mysql -lodb -o driver

这导致了

代码语言:javascript
复制
driver.cxx:(.text+0x14d): undefined reference to `person::person(std::string const&, std::string const&, unsigned short)'
EN

回答 2

Stack Overflow用户

发布于 2016-09-08 02:20:04

你需要添加构造函数的实现。示例:

代码语言:javascript
复制
person (const std::string& first,
      const std::string& last,
      unsigned short age){}
票数 0
EN

Stack Overflow用户

发布于 2017-11-02 21:06:22

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

替换以下内容:

代码语言:javascript
复制
  person (const std::string& first,
          const std::string& last,
          unsigned short age);

有了这个:

代码语言:javascript
复制
  person (const std::string& first,
          const std::string& last,
          unsigned short age) :
      first_(first),
      last_(last),
      age_(age)
  {
  }

此外,在driver.cxx中,您可以将auto_ptr替换为unique_ptr,或者使用以下方式进行编译:

代码语言:javascript
复制
g++ -g -std=c++98 -o driver driver.cxx person-odb.cxx -lodb-mysql -lodb

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29420228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档