Why does namespace usage break MinGW compilation? - c++

Through the act of separating a set of input and output related functions from other parts of a program, I have encountered a problem with compiling files when functions in a header are placed within a namespace. The following files compile:
main.cpp
#include "IO.h"
int main()
{
testFunction("yikes");
}
IO.h
#ifndef IO_H_INCLUDED
#define IO_H_INCLUDED
#include <string>
void testFunction(const std::string &text);
#endif
However, when testFunction is placed in a namespace:
#ifndef IO_H_INCLUDED
#define IO_H_INCLUDED
#include <string>
// IO.h
namespace IO
{
void testFunction(const std::string &text);
}
#endif
within IO.h, and then invoked as IO::testFunction, compilation fails, throwing
undefined reference to `IO::testFunction(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2.exe: error: ld returned 1 exit status`
In each case, IO.cpp is
#include <string>
#include <iostream>
void testFunction(const std::string &text)
{
std::cout << text << std::endl;
}
and the compilation command is g++ -std=c++11 main.cpp IO.cpp, with the compiler being x86_64-w64-mingw32 from TDM-GCC, on Windows 10 Home.

If you change the declaration of your function to be in a namespace you need to implement the function within this namespace aswell.
The signature of your function will be IO::testFunction(...) but you only implemented testFunction(...) so there is no implementation for IO::testFunction(...)
The header file (IO.h):
namespace IO {
void testFunction(const std::string &text);
}
The cpp file (IO.cpp):
#include "IO.h"
// either do this
namespace IO {
void testFunction(const std::string &text) { ... }
// more functions in namespace
}
// or this
void IO::testFunction(const std::string &text) { ... }

Related

C++ undefined namespace

I'm trying to create a header file, implementation file and main file for a simple c++ program that returns the systems hostname using gethostname().
my header file that declares my class, data, and methods
//hostname.h
#pragma once
namespace hostnamespace
{
class hostname_class
{
public:
hostname_class();
void to_string();
private:
char *name;
};
}
my implementation file that defines my class and methods
//hostname_class.cpp
#include "hostname.h"
#include <iostream>
#include <unistd.h>
#include <limits.h>
using namespace hostnamespace;
using namespace std;
class hostname_class{
private:
char *name;
public:
hostname_class(){
gethostname(name, HOST_NAME_MAX);
}
void to_string(){
cout << "Hostname:" << &name << endl;
}
};
my main program file
//hostname_main.cpp
#include "hostname.h"
#include <iostream>
using namespace hostnamespace;
int main() {
hostname_class host;
host.to_string();
return 0;
}
when I try and run g++ -o main hostname_main.cpp hostname_class.cpp
I get this error
/bin/ld: /tmp/ccGfbyuu.o: in function `main':
hostname_main.cpp:(.text+0x1f): undefined reference to `hostnamespace::hostname_class::hostname_class()'
/bin/ld: hostname_main.cpp:(.text+0x2b): undefined reference to `hostnamespace::hostname_class::to_string()'
collect2: error: ld returned 1 exit status
any help would be appreciated.
You are redeclaring the hostname_class in the global namespace instead of defining it within the namespace you intended.
A sample structure of how your hostname_class.cpp file could be written.
#include <iostream>
#include <unistd.h>
#include <limits.h>
#include "hostname.h"
using namespace std;
namespace hostnamespace
{
hostname_class::hostname_class() {
gethostname(name, HOST_NAME_MAX);
}
void hostname_class::to_string() {
cout << "Hostname:" << name << endl;
}
};
One side note - I literally copied your method implementations verbatim. It will compile. But name is certainly uninitialized and pointing to an undefined memory address when you pass it into gethostname. That's probably not good.

Including header files in C++ (class definition and method implementation)

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp

c++ 'undefined reference to' error

I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}

odb/pgsql/version.hxx no such file or directory

I'm trying to learn how to use C++ and ODB following this tutorial:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2
I've created a Person.hxx file where there is the declaration of class Person as persistent, then I've got thre files Person-odb: .cxx, .hxx, .ixx
Now I should compile Person-odb.cxx with
g++ -I/usr/lib/odb/i686-linux-gnu/include Person-odb.cxx
but it end with:
fatal error: odb/pgsql/version.hxx: No such file or directory. compilation terminated.
I see that there is a file version.hxx but there's no odb/pgsql directory...
what's wrong?
this is Person.hxx where I have defined the persistent class 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
then I must compile Person.hxx with odb compiler:
odb -d mysql --generate-query --generate-schema Person.hxx
and I get 4 files Person.odb.hxx, .cxx, .sql, .ixx
this is driver.cxx where I have the main program which persists objects:
#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;
}
}
and this is the file 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
everything seems to work fine when I perform:
c++ -c Person-odb.cxx
c++ -c driver.cxx
but in the end when I have to link all together with:
c++ -o driver driver.o Person-odb.o -lodb-mysql -lodb
I get:
"undefined reference to `Person::Person(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, unsigned short)'"
What seems to be your problem is that you didn't install ODB. The Installing ODB page should get you up and running.
You see a version.hxx file, but that's an input file for the ODB compilation process, not for including in your programs.
If you did install it, find out where on your system and add that folder to your compiler include path. Your compilation command then becomes
g++ -I/usr/lib/odb/i686-linux-gnu/include -I/path/to/odb/install/include Person-odb.cxx
Following your edits, I think the issue is that you're not linking to the Person object file, only the Person-odb one.
Compile Person.cxx with
g++ -c Person.cxx
and change your linking command to
g++ -o driver driver.o Person.o Person-odb.o -lodb-mysql -lodb
and the error should be fixed.
Please note that I don't know ODB. I'm trying to figure this out from a pure C++ perspective.

C++ Simple compile error

I am having a Compile issue.
I have one Class
I have one header file
And of course Main to test my work.
But I am getting compile error, it is out of my understanding what I am doing wrong.
Header File:
#ifndef AGENT_H
#define AGENT_H
using namespace std;
class Agent
{
public:
Agent(string);
virtual ~Agent();
private:
string name;
};
#endif /* AGENT_H */
Agent Class (Agent.cpp)
#include "Agent.h"
using namespace std;
Agent::Agent(string _name)
{
this->name = _name;
}
Agent::~Agent()
{
delete this->name;
}
And my Main:
#include <cstdlib>
#include <iostream>
#include "Agent.h"
using namespace std;
int main(int argc, char** argv)
{
Agent agent1("Danila");
return 0;
}
So I am getting such strange error:
undefined reference to `Agent::Agent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/main.cpp:17: undefined reference to `Agent::~Agent()'
/main.cpp:17: undefined reference to `Agent::~Agent()'
Could you guys help me understand whats wrong there?
You need an #include <string> in your header file.
Also, for good practice, keep the using namespaces in your .cpp files, if any.
You compiled without telling the compiler about Agent.cpp. I.e. you need something like this, for g++:
$ g++ main.cpp Agent.cpp -o myprogram