0

I am trying to compile the "Hello World" example that comes with odb. I am using debian Linux.

I copied the person.hxx and driver.cxx files

// 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 compiler worked fine and produced person-odb files.

I compiled them with

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

and all went well.

The problem started with the link phase

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

which reulted in

driver.cxx:(.text+0x14d): undefined reference to `person::person(std::string const&, std::string const&, unsigned short)'
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Ran Levy
  • 135
  • 9
  • 3
    you either don't compile and also link the `person.cpp`, or you don't define the constructor `person::person`. The linker cannot find the object code for the constructor. – vsoftco Apr 02 '15 at 18:49
  • where is person-odb.cxx? Show it to us. THe linker is complaining that it doesnt contain the code for the constructor – pm100 Apr 02 '15 at 18:52
  • The example does not have a person.cpp file, only a file named "person-odb.cpp" which is automatically generated by the odb compiler – Ran Levy Apr 02 '15 at 18:53
  • who wrote person.hxx, the same person needs to write a person.cxx – pm100 Apr 02 '15 at 18:58
  • 2
    You need to run the `odb` compiler to generate the `person-odb.cxx` file. This file needs to be compiled and linked with the other files. – Thomas Matthews Apr 02 '15 at 19:35
  • I ran the odb compiler with `odb -d mysql --generate-query person.hxx` and it generated person-odb.cxx. The problem is that for some reason it will not link – Ran Levy Apr 03 '15 at 03:05
  • Please cut and paste the actual lines you used. You have writen `g++ -c deiver.cxx` which is obviously incorrect spelling. You may have made a typo. – Mark Lakata Sep 07 '16 at 18:26
  • What namespace is used in the generated `person-odb.cpp` or `person-odb.hxx` ? Is the name of the generated class `person` or something else? – Mark Lakata Sep 07 '16 at 18:29

2 Answers2

0

You're need add implementation of constructor. Example:

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

After a couple of years you can still have problems if you copy and paste the step by step example given in the odb website. The implementation is missing in the person.hxx file.

Replace following:

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

with this:

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

Additionally in driver.cxx you can replace the auto_ptr with unique_ptr or compile this way:

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

You can download the working examples from: https://www.codesynthesis.com/products/odb/download.xhtml

jav
  • 583
  • 5
  • 15