GCC 4.5.2 Linker gots Problem while using Exceptions (C++) - c++

I try to write a webserver. As far as I am, it works quiet good with Windows. But I want to make it also Unix compatible. And I think there has to be a problem with the heredity f the exception class.
For better understanding, just the important parts:
server.cpp
#include <exception>
#include <stdexcept>
#ifdef __unix__
#include "UnixSocket.h"
#elif __WIN32__ || _MSC_VER
#include "WinSocket.h"
#endif
#include "HTTPParser.h"
int main(void) {
try {
socket->socketInit(PORT);
}
catch (exception &e) {
cout << endl << "Exception: " << e.what() << endl;
socket->cleanAll();
}
return 0
}
NetInterface.h
class NetInterface : public exception {
private:
public:
virtual void socketInit(const char *port) = 0;
virtual void cleanAll(void) = 0;
virtual void cleanPersCon(void) = 0;
virtual char *akzeptieren(void) = 0;
virtual void empfangen(void) = 0;
virtual void senden(void) = 0;
virtual void *get_in_addr(struct sockaddr *sa) = 0;
virtual string getIncoming(void) = 0;
virtual void setOutcoming(string s) = 0;
virtual ~NetInterface() throw() {};
};
UnixSocket.h
class UnixSocket : virtual public NetInterface {
private:
[...]
public:
UnixSocket(void);
//kill socket connections
void cleanAll(void);
void cleanPersCon(void);
//SysCalls
void socketInit(const char *port);
char *akzeptieren(void);
void empfangen(void);
void senden(void);
//Getter and Setter
string getIncoming(void);
void setOutcoming(string s);
virtual ~UnixSocket() throw() {};
};
HTTPParser.h
class HTTPParser : public exception {
private:
[...]
public:
HTTPParser(NetInterface *_socket, string _path);
void parsePacket(void);
virtual ~HTTPParser() throw() {};
};
There you can se a short summary of the class declarations.
But now the gcc tells me something like this:
/tmp/cc8DNmKI.o:(.rodata._ZTV10HTTPParser[vtable for HTTPParser]+0x10): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV10UnixSocket[vtable for UnixSocket]+0x14): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV10UnixSocket[vtable for UnixSocket]+0x78): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x10): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x14): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x18): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x1c): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x20): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x24): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x28): more undefined references to `__cxa_pure_virtual' follow
/tmp/cc8DNmKI.o:(.rodata._ZTVSt16invalid_argument[vtable for std::invalid_argument]+0x10): undefined reference to `std::logic_error::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTVSt12domain_error[vtable for std::domain_error]+0x10): undefined reference to `std::logic_error::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTI10HTTPParser[typeinfo for HTTPParser]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI10HTTPParser[typeinfo for HTTPParser]+0x8): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTI10UnixSocket[typeinfo for UnixSocket]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI10UnixSocket[typeinfo for UnixSocket]+0x18): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTI12NetInterface[typeinfo for NetInterface]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI12NetInterface[typeinfo for NetInterface]+0x8): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTISt16invalid_argument[typeinfo for std::invalid_argument]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTISt16invalid_argument[typeinfo for std::invalid_argument]+0x8): undefined reference to `typeinfo for std::logic_error'
/tmp/cc8DNmKI.o:(.rodata._ZTISt12domain_error[typeinfo for std::domain_error]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTISt12domain_error[typeinfo for std::domain_error]+0x8): undefined reference to `typeinfo for std::logic_error'
/tmp/cc8DNmKI.o:(.eh_frame+0xeb): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
and even more....

Judging from undefined reference to __gxx_personality_v0 linker message it looks like you are linking with gcc. You need to link C++ applications with g++. Or link with gcc and add -lstdc++ to the linker command line.

You aren't showing the relevant code and compiler invocation, but know that std::exception has an unimplemented virtual member function what() that you're expected to override, so don't go throwing naked std::exceptions if you want to callwhat().
Any of the derived, specific exceptions in <stdexcept> will implement what() and allow you to store a message when you're constructing the exception object.

Related

Compile error when initializing class member std::function?

I am trying to create a class with an std::function member:
# include<functional>
class Widget {
public:
std::function<int(double)> call_foo;
Widget(std::function<int(double)> call_func)
: call_foo(call_func)
{};
};
However, when I try to initialize the class my code fails:
const int f(double x){
if(x > 5.0){
return 22;
}
return 17;
}
int main()
{
std::function<int(double)> f1 = f;
Widget p(f1);
}
The full set of errors is below:
tmp/ccDoRHCh.o: In function `__static_initialization_and_destruction_0(int, int)':
widget.cpp:(.text+0x109): undefined reference to `std::ios_base::Init::Init()'
widget.cpp:(.text+0x118): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccDoRHCh.o:(.rodata._ZTIPFidE[_ZTIPFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__pointer_type_info'
/tmp/ccDoRHCh.o:(.rodata._ZTIFidE[_ZTIFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info'
/tmp/ccDoRHCh.o:(.eh_frame+0xab): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I am compiling with C++ 11 enabled.
Chances are either you forgot
#include <functional>
or you'r not compiling in C++11 mode.

Issue linking libxml++ and glib libraries with CodeBlocks IDE for C++ Windows

I am writing a C++ app for Windows using Code Blocks IDE. I am interested in using the following XML++ library: http://libxmlplusplus.sourceforge.net/
It requires libxml2 and glibmm-2.4 libraries. I downloaded the source for each of these libraries and included all of the headers into my project by right clicking on "Build Options" ==> "Search Directories" tab ==> "Compiler" tab. I specified the header include files there. I modified the main.cpp file using the source code from "examples/dom_parser" from the xml++/examples directory.
Now, I am having trouble with the following error message. I have never "linked" or used *.lib, *.dll files before... but I am now getting the following "undefined reference" error messages. Please let me know what I'd need to do in order to build this. Is there a particular file that I need to "link" and if so, where are these files located? I can't seem to find them in the source files that I extracted. Could you help with specific instructions on which files to include and which folders they might be located in? I am using the CodeBlocks IDE.
Could someone replicate the project on your Windows 64 bit PC and see if it is able to run correctly?
Thank you.
UPDATE
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:44: undefined reference to `xmlpp::ContentNode::is_white_space() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:47: undefined reference to `xmlpp::Node::get_name() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:49: undefined reference to `Glib::ustring::empty() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:53: undefined reference to `xmlpp::Node::get_namespace_prefix() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:54: undefined reference to `Glib::ustring::empty() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:55: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:57: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:57: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:57: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:69: undefined reference to `xmlpp::ContentNode::get_content() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:69: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:69: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:74: undefined reference to `xmlpp::ContentNode::get_content() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:74: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:74: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:79: undefined reference to `xmlpp::ContentNode::get_content() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:79: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:79: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:87: undefined reference to `xmlpp::Node::get_line() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:90: undefined reference to `xmlpp::Element::get_attributes() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:96: undefined reference to `xmlpp::Node::get_namespace_prefix() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:97: undefined reference to `Glib::ustring::empty() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `xmlpp::Attribute::get_value() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `xmlpp::Attribute::get_name() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:98: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `xmlpp::Attribute::get_value() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `xmlpp::Attribute::get_name() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:100: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:101: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:103: undefined reference to `Glib::ustring::ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:103: undefined reference to `Glib::ustring::ustring(char const*)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:103: undefined reference to `xmlpp::Element::get_attribute(Glib::ustring const&, Glib::ustring const&) const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:103: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:103: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:106: undefined reference to `xmlpp::Attribute::get_value() const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:106: undefined reference to `Glib::operator<<(std::ostream&, Glib::ustring const&)'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:106: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:113: undefined reference to `Glib::ustring::ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:113: undefined reference to `xmlpp::Node::get_children(Glib::ustring const&) const'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:113: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:118: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:57: undefined reference to `Glib::ustring::~ustring()'
C:/Users/blah/Desktop/workspace/xmlpp/main.cpp:69: undefined reference to `Glib::ustring::~ustring()'
**UPDATED 02/11/2014 - at 10:45am **
Hi. Thanks for your suggestion. I ended up downloading the following ( http://ftp.gnome.org/pub/gnome/binaries/win32/gtkmm/2.22/ ) the entire gtkmm-win32-devel-2.22.0-2.exe and installed it onto my Windows PC in C:\gtkmm. Then, I modified my project by including the header files, library files, and bin files. Here are the screen shots:
After building, I am now seeing 0 errors and 0 warning messages. However, it appears to CRASH. I have no idea why. It seems that the gtk installation uses libxml++ version 2.6. This is fine. I downloaded the libxml++ 2.6 from the website to see the examples that they provided. I used the following source code in my main.cc. Do you know what is the problem?
// -*- C++ -*-
/* main.cc
*
* Copyright (C) 2002 The libxml++ development team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libxml++/libxml++.h>
#include <iostream>
void print_indentation(unsigned int indentation)
{
for(unsigned int i = 0; i < indentation; ++i)
std::cout << " ";
}
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
{
std::cout << std::endl; //Separate nodes by an empty line.
const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
return;
Glib::ustring nodename = node->get_name();
if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
{
print_indentation(indentation);
std::cout << "Node name = " << node->get_name() << std::endl;
std::cout << "Node name = " << nodename << std::endl;
}
else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
{
print_indentation(indentation);
std::cout << "Text Node" << std::endl;
}
//Treat the various node types differently:
if(nodeText)
{
print_indentation(indentation);
std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
}
else if(nodeComment)
{
print_indentation(indentation);
std::cout << "comment = " << nodeComment->get_content() << std::endl;
}
else if(nodeContent)
{
print_indentation(indentation);
std::cout << "content = " << nodeContent->get_content() << std::endl;
}
else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
{
//A normal Element node:
//line() works only for ElementNodes.
print_indentation(indentation);
std::cout << " line = " << node->get_line() << std::endl;
//Print attributes:
const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
{
const xmlpp::Attribute* attribute = *iter;
print_indentation(indentation);
std::cout << " Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
}
const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
if(attribute)
{
std::cout << "title found: =" << attribute->get_value() << std::endl;
}
}
if(!nodeContent)
{
//Recurse through child nodes:
xmlpp::Node::NodeList list = node->get_children();
for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
{
print_node(*iter, indentation + 2); //recursive
}
}
}
int main(int argc, char* argv[])
{
Glib::ustring filepath;
if(argc > 1 )
filepath = argv[1]; //Allow the user to specify a different XML file to parse.
else
filepath = "example.xml";
try
{
xmlpp::DomParser parser;
parser.set_validate();
parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
parser.parse_file(filepath);
if(parser)
{
//Walk the tree:
const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
print_node(pNode);
}
}
catch(const std::exception& ex)
{
std::cout << "Exception caught: " << ex.what() << std::endl;
}
return 0;
}
Are libxml2 and glibmm-2.4 built?
You can find prebuilt windows binaries for libxml2 here. I can't find prebuilt windows binaries for glibmm, so you'll either have to download the entire gtkmm installer, or build it yourself.
After this process is done, you should have files such as
libxml2.lib
libxml2.a
libxml2.dll
Depending on you compiler (GCC or MSVC), and the desired linking method (static or dynamic), you should choose the required library files.
In Code::Blocks you do the linking via the "Linker settings" tab, accessible through Project -> Build options... -> Linker settings. Click add and select the previously compiled
library files (libxml2, glibmm-2.4 and libxml++) using the folder browser.
Update: I am able to successfully run the project on my Windows 8 64-bit PC.
I have a MinGW-64 installation from MinGW-builds (GCC 4.8.1). The only other thing I had to download was the gtkmm-win64 installer. I chose the "full installation".
The search directories in the project build settings were:
C:\gtkmm64\include
C:\gtkmm64\include\libxml++-2.6
C:\gtkmm64\include\glibmm-2.4
C:\gtkmm64\include\glib-2.0
C:\gtkmm64\lib\glibmm-2.4\include
C:\gtkmm64\lib\glib-2.0\include
C:\gtkmm64\lib\libxml++-2.6\include
The import libraries to link to were:
C:\gtkmm64\lib\libxml++-2.6.dll.a
C:\gtkmm64\lib\libglibmm-2.4.dll.a
C:\gtkmm64\lib\libglibmm_generate_extra_defs-2.4.dll.a
And the DLL's copied to the executable's location from the gtkmm64\bin folder were:
libglib-2.0-0.dll
libglibmm-2.4-1.dll
libglibmm_generate_extra_defs-2.4-1.dll
libxml++-2.6-2.dll
libxml2-2.dll
The executable ran successfully (excluding the fact that the program terminated due to some parsing exception).

QtCreator and Poco Unable to Compile Project On Linux

I am trying to experiment with the Poco library in order to send an email.
However when I try using the library I get an undefined error.
I think it has something to do with the compiler I used for Poco, but I am unsure which compiler I should use so that QT and Poco are compiled with the same thing.
Mass_Mailer.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Mass_Mailer
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += /usr/local/include/
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/MailRecipient.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/NetException.h>
using namespace std;
using namespace Poco::Net;
using namespace Poco;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
int sendmail()
{
string host = "mail.domain.com";
UInt16 port = 25;
string user = "xxx";
string password = "xxx";
string to = "xxx#domain.com";
string from = "xxx#domain.com";
string subject = "Your first e-mail message sent using Poco Libraries";
subject = MailMessage::encodeWord(subject.c_str(), "UTF-8");
string content = "Well done! You've successfully sent your first message using Poco SMTPClientSession";
MailMessage message;
message.setSender(from);
message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, to));
message.setSubject(subject);
message.setContentType("text/plain; charset=UTF-8");
message.setContent(content, MailMessage::ENCODING_8BIT);
try {
SMTPClientSession session(host, port);
session.open();
try {
session.login(SMTPClientSession::AUTH_LOGIN, user, password);
session.sendMessage(message);
cout << "Message successfully sent" << endl;
session.close();
} catch (SMTPException &e) {
cerr << e.displayText() << endl;
session.close();
return 0;
}
} catch (NetException &e) {
cerr << e.displayText() << endl;
return 0;
}
return 0;
}
Errors
main.o: In function `sendmail()':
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:31: undefined reference to `Poco::Net::MailMessage::encodeWord(std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:34: undefined reference to `Poco::Net::MailMessage::setSender(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::MailRecipient(Poco::Net::MailRecipient::RecipientType, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailMessage::addRecipient(Poco::Net::MailRecipient const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:36: undefined reference to `Poco::Net::MailMessage::setSubject(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:37: undefined reference to `Poco::Net::MailMessage::setContentType(std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:38: undefined reference to `Poco::Net::MailMessage::setContent(std::string const&, Poco::Net::MailMessage::ContentTransferEncoding)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:40: undefined reference to `Poco::Net::SMTPClientSession::SMTPClientSession(std::string const&, unsigned short)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:41: undefined reference to `Poco::Net::SMTPClientSession::open()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:43: undefined reference to `Poco::Net::SMTPClientSession::login(Poco::Net::SMTPClientSession::LoginMethod, std::string const&, std::string const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:44: undefined reference to `Poco::Net::SMTPClientSession::sendMessage(Poco::Net::MailMessage const&)'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:46: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::~MailMessage()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:35: undefined reference to `Poco::Net::MailRecipient::~MailRecipient()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:48: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:49: undefined reference to `Poco::Net::SMTPClientSession::close()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:51: undefined reference to `Poco::Net::SMTPClientSession::~SMTPClientSession()'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:53: undefined reference to `Poco::Exception::displayText() const'
/home/mongo/Cpp/Mass_Mailer/build-Mass_Mailer-Desktop-Debug/../Mass_Mailer/main.cpp:33: undefined reference to `Poco::Net::MailMessage::~MailMessage()'
main.o:(.gcc_except_table+0x120): undefined reference to `typeinfo for Poco::Net::SMTPException'
main.o:(.gcc_except_table+0x124): undefined reference to `typeinfo for Poco::Net::NetException'
collect2: error: ld returned 1 exit status
make: *** [Mass_Mailer] Error 1
11:45:01: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Mass_Mailer (kit: Desktop)
When executing step 'Make'11:45:01: Elapsed time: 00:02.
You need to link Poco libraries to your project. In the project file, add something like this:
LIBS += -lPocoNet -lPocoFoundation

Linking D library to C++ code

Recently I learned the beautiful language D, which is so more plastic and helps yourself writing stable fast programs. But its not popular... because few code written on D and so more on C and C++. Therefore after I read the book of Andrei Alexanderscu where author very superficially described question about linking of D library to C++ code, I tried learn it myself and written some code on D where defined function that returns an instance of CompleteAutomata class which implements AutomataInterface defined also in C++ code:
#ifndef AUTOMATAINTERFACE_H
#define AUTOMATAINTERFACE_H
class AutomataInterface {
public:
virtual ~AutomataInterface() {}
virtual void next() = 0;
virtual void save() = 0;
virtual void restore() = 0;
virtual void zerofile() = 0;
virtual void invert(unsigned long x, unsigned long y) = 0;
virtual int state(unsigned long x, unsigned long y) const = 0;
virtual unsigned long x() const = 0;
virtual unsigned long y() const = 0;
};
AutomataInterface *createAutomata(unsigned long x, unsigned long y);
#endif // AUTOMATAINTERFACE_H
Relevant D code:
import agregator; // this is my own lib
extern(C++) {
interface AutomataInterface {
void next();
void save();
void restore();
void zerofile();
void invert(size_t x, size_t y);
int state(size_t x, size_t y) const;
size_t x() const;
size_t y() const;
}
AutomataInterface createAutomata(ulong x, ulong y) {
return new CompleteAutomata(x, y);
}
}
export class CompleteAutomata : AutomataInterface {
// instance variables...
this(size_t x, size_t y) { /* ... */ }
extern(C++) {
override void next() {
// ...
}
// others overridden interface methods...
}
}
After code had written, I've compiling of D library by two different compilers (dmd and gdc), with following flags:
dmd -release -O -lib -odlib -ofliblife.h *.d
or
gdc -frelease -O2 -Wall -c *.d
ar cq lib/liblife.a *.o
When I trying link each of received libs to Qt project by adding path to library dir (-L option) and adding a lib directly (-l option). I got errors of in both cases.
In first dmd case I have "undefined reference to `_d_newclass'" and couple of another errors:
g++ -Wl,-O1 -Wl,-z,relro -o automata main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1fe_5b0.o): In function `createAutomata(unsigned int, unsigned int)':
complete_automata.d:(.text._Z14createAutomatajj+0x27): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x50): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0xd0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x120): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x68): undefined reference to `_D6object6Object8toStringMFZAya'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x70): undefined reference to `_D6object6Object6toHashMFNbNeZm'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x78): undefined reference to `_D6object6Object5opCmpMFC6ObjectZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x80): undefined reference to `_D6object6Object8opEqualsMFC6ObjectZb'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0xf8): undefined reference to `_D6object6Object8toStringMFZAya'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x100): undefined reference to `_D6object6Object6toHashMFNbNeZm'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x108): undefined reference to `_D6object6Object5opCmpMFC6ObjectZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x110): undefined reference to `_D6object6Object8opEqualsMFC6ObjectZb'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `_D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata':
complete_automata.d:(.text._D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata+0x1f): undefined reference to `_d_newclass'
complete_automata.d:(.text._D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata+0x46): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::next()':
complete_automata.d:(.text._ZN16CompleteAutomata4nextEv+0x2f): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::save()':
complete_automata.d:(.text._ZN16CompleteAutomata4saveEv+0x25): undefined reference to `_adDupT'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::restore()':
complete_automata.d:(.text._ZN16CompleteAutomata7restoreEv+0x33): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o): In function `CompleteAutomata::zerofile()':
complete_automata.d:(.text._ZN16CompleteAutomata8zerofileEv+0x2f): undefined reference to `_d_newclass'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `no symbol':
/usr/include/dmd/druntime/import/object.di:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o):(.data._D12TypeInfo_Axi6__initZ+0x0): undefined reference to `_D14TypeInfo_Array6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5137__arrayZ':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5137__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5138__assertFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5138__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(object_201_8b7.o): In function `_D46/usr/include/dmd/druntime/import/object.di.51315__unittest_failFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.51315__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `no symbol':
/usr/include/dmd/druntime/import/object.di:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o):(.data._D11TypeInfo_xi6__initZ+0x0): undefined reference to `_D14TypeInfo_Const6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o):(.data._D11TypeInfo_xi6__initZ+0x10): undefined reference to `_D10TypeInfo_i6__initZ'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5157__arrayZ':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5157__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.5158__assertFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.5158__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(object_203_875.o): In function `_D46/usr/include/dmd/druntime/import/object.di.51515__unittest_failFiZv':
/usr/include/dmd/druntime/import/object.di:(.text._D46/usr/include/dmd/druntime/import/object.di.51515__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `no symbol':
agregator.d:(.text+0x6): undefined reference to `_Dmodule_ref'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o):(.data+0x10): undefined reference to `_D3std6random12__ModuleInfoZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o):(.rodata+0x20): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator7__arrayZ':
agregator.d:(.text._D9agregator7__arrayZ+0x16): undefined reference to `_d_array_bounds'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator8__assertFiZv':
agregator.d:(.text._D9agregator8__assertFiZv+0x16): undefined reference to `_d_assertm'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D9agregator15__unittest_failFiZv':
agregator.d:(.text._D9agregator15__unittest_failFiZv+0x16): undefined reference to `_d_unittestm'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.data+0x0): undefined reference to `_D14TypeInfo_Class6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.data+0x50): undefined reference to `_D6Object7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(agregator_2_5fd.o):(.rodata+0x48): undefined reference to `_D6object6Object8toStringMFZAya'
...
In second case (when using gdc) I receives message about "multiple definition of":
g++ -Wl,-O1 -Wl,-z,relro -o cellular_life main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `_D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi':
complete_automata.d:(.text+0x0): multiple definition of `_D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::invert(unsigned long long, unsigned long long)':
complete_automata.d:(.text+0x40): multiple definition of `CompleteAutomata::invert(unsigned long long, unsigned long long)'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZN16CompleteAutomata6invertEyy+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::state(unsigned long long, unsigned long long) const':
complete_automata.d:(.text+0x60): multiple definition of `CompleteAutomata::state(unsigned long long, unsigned long long) const'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZNK16CompleteAutomata5stateEyy+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::x() const':
complete_automata.d:(.text+0x80): multiple definition of `CompleteAutomata::x() const'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZNK16CompleteAutomata1xEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::y() const':
complete_automata.d:(.text+0xa0): multiple definition of `CompleteAutomata::y() const'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZNK16CompleteAutomata1yEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::next()':
complete_automata.d:(.text+0x140): multiple definition of `CompleteAutomata::next()'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZN16CompleteAutomata4nextEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.tbss+0x10): multiple definition of `_D17complete_automata16CompleteAutomata4nextMRZv7changerC7changer7Changer'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.tbss+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::restore()':
complete_automata.d:(.text+0x1b0): multiple definition of `CompleteAutomata::restore()'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZN16CompleteAutomata7restoreEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.tbss+0x8): multiple definition of `_D17complete_automata16CompleteAutomata7restoreMRZv9agregatorC9agregator9Agregator'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.tbss+0x8): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.data+0x180): multiple definition of `_D_ZN16CompleteAutomata7restoreEv14SliceAgregator7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::zerofile()':
complete_automata.d:(.text+0x220): multiple definition of `CompleteAutomata::zerofile()'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZN16CompleteAutomata8zerofileEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.tbss+0x0): multiple definition of `_D17complete_automata16CompleteAutomata8zerofileMRZv9agregatorC9agregator9Agregator'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.tbss+0x10): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::save()':
complete_automata.d:(.text+0x290): multiple definition of `CompleteAutomata::save()'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._ZN16CompleteAutomata4saveEv+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.data+0x80): multiple definition of `_D17complete_automata16CompleteAutomata7__ClassZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.data+0xd0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `_D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata':
complete_automata.d:(.text+0x9b0): multiple definition of `_D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):complete_automata.d:(.text._D17complete_automata16CompleteAutomata6__ctorMFmmZC17complete_automata16CompleteAutomata+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.rodata+0x420): multiple definition of `_D17complete_automata16CompleteAutomata6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0xf0): first defined here
/usr/bin/ld: Warning: size of symbol `_D17complete_automata16CompleteAutomata6__vtblZ' changed from 104 in /home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o) to 112 in /home/newmen/projects/d/life/lib/liblife.a(complete_automata.o)
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.rodata+0x4a0): multiple definition of `_D17complete_automata16CompleteAutomata6__initZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x90): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.rodata+0x4e0): multiple definition of `_D_ZN16CompleteAutomata7restoreEv14SliceAgregator6__vtblZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x60): first defined here
/usr/bin/ld: Warning: size of symbol `_D_ZN16CompleteAutomata7restoreEv14SliceAgregator6__vtblZ' changed from 48 in /home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o) to 56 in /home/newmen/projects/d/life/lib/liblife.a(complete_automata.o)
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o):(.rodata+0x520): multiple definition of `_D_ZN16CompleteAutomata7restoreEv14SliceAgregator6__initZ'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1ff_675.o):(.rodata+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D3std7complex14__T7ComplexTeZ7Complex8toStringMxFMDFAxaZvAyaZAya12__lambda1223MFNbNfAxaZv':
agregator.d:(.text+0xaf): undefined reference to `_D11TypeInfo_Aa6__initZ'
agregator.d:(.text+0xb7): undefined reference to `_d_arrayappendT'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D3std4conv16__T6toImplTiTxkZ6toImplFNaNfxkZi15__dgliteral1389MFNaNfZC6object9Throwable':
agregator.d:(.text+0xc5): undefined reference to `_D3std4conv21ConvOverflowException7__ClassZ'
agregator.d:(.text+0xca): undefined reference to `_d_newclass'
agregator.d:(.text+0xed): undefined reference to `_D3std4conv21ConvOverflowException6__ctorMFAyaAyamZC3std4conv21ConvOverflowException'
/home/newmen/projects/d/life/lib/liblife.a(agregator.o): In function `_D3std6format17__T9getNthIntTxeZ9getNthIntFNaNfkxeZi.part.6':
agregator.d:(.text+0x105): undefined reference to `_D3std6format15FormatException7__ClassZ'
agregator.d:(.text+0x10a): undefined reference to `_d_newclass'
...
After two days of attempts to do so...
Recently I've try add Phobos (D standard library) to linking process. For dmd -lphobos2 flag and for gdc -lgphobos2 flag correspond. But it not help me...
When using dmd linker output:
g++ -Wl,-O1 -Wl,-z,relro -o cellular_life main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread -lphobos2
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_duphandle#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_strerror#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_slist_free_all#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_global_init#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_perform#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_init#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_pause#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `_Dmain'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_setopt#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_slist_append#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_global_cleanup#CURL_GNUTLS_3'
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/libphobos2.so: undefined reference to `curl_easy_cleanup#CURL_GNUTLS_3'
collect2: error: ld returned 1 exit status
make: *** [cellular_life] Error 1
and I've try substitute of libcurl-gnutls: ln -s /usr/lib64/libcurl.so.4 /usr/lib64/libcurl-gnutls.so.4. Then result of linking the same but without message about libcurl-gnutls.
When using gdc linker output again talk about "multiple definition to":
/home/newmen/gcc/bin/g++ -Wl,-O1 -Wl,-z,relro -o cellular_life main.o mainwindow.o renderarea.o button.o playbutton.o moc_mainwindow.o moc_renderarea.o moc_button.o moc_playbutton.o -L/home/newmen/gcc/lib64 -L/home/newmen/projects/d/life/lib -llife -lQtGui -lQtCore -lpthread -lgphobos2
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `_D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi':
complete_automata.d:(.text+0x0): multiple definition of `_D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1e3_675.o):complete_automata.d:(.text._D17complete_automata16CompleteAutomata7restoreMRZv14SliceAgregator9initValueMxFmmZi+0x0): first defined here
/home/newmen/projects/d/life/lib/liblife.a(complete_automata.o): In function `CompleteAutomata::invert(unsigned long long, unsigned long long)':
complete_automata.d:(.text+0x40): multiple definition of `CompleteAutomata::invert(unsigned long long, unsigned long long)'
/home/newmen/projects/d/life/lib/liblife.a(complete_automata_1e3_675.o):complete_automata.d:(.text._ZN16CompleteAutomata6invertEyy+0x0): first defined here
...
/home/newmen/gcc/lib64/libgphobos2.a(dmain2.o): In function `main':
/home/newmen/projects/distrib/gcc-4.8.1/x86_64-unknown-linux-gnu/libphobos/libdruntime/../../.././libphobos/libdruntime/rt/dmain2.d:394: multiple definition of `main'
main.o:/home/newmen/projects/d/life/qt_viewer/main.cpp:5: first defined here
/usr/bin/ld: /home/newmen/gcc/lib64/libgphobos2.a(time.o): undefined reference to symbol 'clock_getres##GLIBC_2.2.5'
/usr/bin/ld: note: 'clock_getres##GLIBC_2.2.5' is defined in DSO /lib64/librt.so.1 so try adding it to the linker command line
/lib64/librt.so.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make: *** [cellular_life] Error 1
with message about librt.so.1 at end. I inspect /usr/lib64 and seen there it library file.
Dear magic, please tell me how to connect the D library to C++ code.
It is generally easier to let the D compiler do the linking:
g++ -c yourfile.cpp
dmd yourfile.o d_file.d
You might have to add curl to it as well, same as you did before. To pass a linker argument through dmd (or gdmd if you're using gdc, should work the same way), pass -Larg
dmd yourfile.o d_file.d -L-lstdc++ -L-lcurl -L-lQtGui # and so on
It is generally easier to put main() in D too (it can just immediately call out to a C++ defined function too) because otherwise you'll probably have to initialize the D runtime before using it from C++.
But to finish the process you've started... first thing, looks like your liblife.a has the same file added twice. I'd try deleting that file and recreating it, or just skipping that step and passing the .o files to the linker directly without first packing them into a .a. That'll simplify things a bit.
My other question is: why is it trying to pull the D main? Is there a main() in your .d code somewhere? If so, that's ok, but you'll have to remove the one from your C++ code. (Perhaps rename it into cppmain and then call it from the D main:
D code:
extern(C++) int cppmain(int argc, char** argv);
int main() {
import core.runtime;
return cppmain(Runtime.cArgs.argc, Runtime.cArgs.argv);
}
And that will forward to your C++ main. If you want to remove the D main (assuming it is there, if not let me know and I'll try to think what else could cause that linker error), before tou use D code in C++, you'll want to initialize D. So:
D code:
extern(C++) void initD() {
import core.runtime;
Runtime.initialize();
}
C++ code:
extern "C++" void initD();
int main() {
initD();
// the rest of your stuff
}
If you don't do that, calling D functions is liable to cause a segfault.
But to sum up, I'm pretty sure you have a duplicate .o file added to your archive, and main defined in both D and C++. Delete the duplicate in the archive and kill one of the duplicate mains and you should have some success.

Calling R function from C++ on Windows

I am trying to call R functions from C++ on Windows. I am using MinGW for compiling the program, but it throws error while compiling. Code (taken from Dirk) and compilation error are as follows:
#include <iostream>
using namespace std;
#include "RInside.h" // for the embedded R via RInside
Rcpp::NumericMatrix createMatrix(const int n) {
Rcpp::NumericMatrix M(n,n);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
M(i,j) = i*10+j;
}
}
return(M);
}
int main(int argc, char *argv[]) {
const int mdim = 4; // let the matrices be 4 by 4
SEXP ans;
RInside R(argc, argv); // create an embedded R instance
Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix
R["M"] = M; // assign C++ matrix M to R's 'M' var
std::string evalstr = "\
cat('Running ls()\n'); print(ls()); \
cat('Showing M\n'); print(M); \
cat('Showing colSums()\n'); Z <- colSums(M); print(Z); \
Z"; // returns Z
ans = R.parseEval(evalstr); // eval the init string -- Z is now in ans
Rcpp::NumericVector v(ans); // convert SEXP ans to a vector of doubles
for (int i=0; i< v.size(); i++) { // show the result
std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
}
return 0;
}
Compile:
g++ -I "C:\ProgramFiles\R\R-2.14.0\library\RInside\include" -I "C:\Progra
mFiles\R\R-2.14.0\library\Rcpp\include" -I "C:\ProgramFiles\R\R-2.14.0\include"
RFunctions.cpp -o sh1.exe
Error:
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x19a): und
efined reference to `RInside::RInside(int, char const* const*, bool)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x1ee): und
efined reference to `RInside::operator[](std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x26d): und
efined reference to `RInside::parseEval(std::string const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x35b): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x3e1): und
efined reference to `RInside::~RInside()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0x8): undefined reference to `vtable for Rcp
p::RObject'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0xd): undefined reference to `_imp__R_NilVal
ue'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN7RInside
5ProxyD1Ev[RInside::Proxy::~Proxy()]+0xd): undefined reference to `Rcpp::RObject
::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED2Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED1Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x57): undefined ref
erence to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x66): undefined ref
erence to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ma
trixILi14EEC1ERKiS3_[Rcpp::Matrix<14>::Matrix(int const&, int const&)]+0x2c): un
defined reference to `Rcpp::Dimension::Dimension(unsigned int const&, unsigned i
nt const&)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp6V
ectorILi14EE4sizeEv[Rcpp::Vector<14>::size() const]+0x10): undefined reference t
o `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0xd): undefined re
ference to `TYPEOF'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0x1d): undefined r
eference to `SEXPREC* Rcpp::internal::r_true_cast<14>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x46): undefined reference to `Rcpp::Dimension::prod() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x56): undefined reference to `Rf_allocVector'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x67): undefined reference to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x7d): undefined reference to `Rcpp::Dimension::size() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0xc9): undefined reference to `Rcpp::RObject::attr(std::string const&) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x13b): undefined reference to `Rcpp::RObject::~RObject()'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp11
Environment6assignINS_6MatrixILi14EEEEEbRKSsRKT_[bool Rcpp::Environment::assign<
Rcpp::Matrix<14> >(std::basic_string<char, std::char_traits<char>, std::allocato
r<char> > const&, Rcpp::Matrix<14> const&) const]+0x23): undefined reference to
`Rcpp::Environment::assign(std::string const&, SEXPREC*) const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bject14AttributeProxyaSINS_9DimensionEEERS1_RKT_[Rcpp::RObject::AttributeProxy&
Rcpp::RObject::AttributeProxy::operator=<Rcpp::Dimension>(Rcpp::Dimension const&
)]+0x1c): undefined reference to `Rcpp::RObject::AttributeProxy::set(SEXPREC*) c
onst'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0xd): undefined reference to `double* Rcpp::internal::r_vector_start<1
4, double>(SEXPREC*)'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0x23): undefined reference to `Rf_length'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal21wrap_dispatch_unknownINS_9DimensionEEEP7SEXPRECRKT_NS_6traits17integral_
constantIbLb1EEE[SEXPREC* Rcpp::internal::wrap_dispatch_unknown<Rcpp::Dimension>
(Rcpp::Dimension const&, Rcpp::traits::integral_constant<bool, true>)]+0xd): und
efined reference to `Rcpp::Dimension::operator SEXPREC*() const'
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6tr
aits14r_vector_cacheILi14EE6updateERKNS_6VectorILi14EEE[Rcpp::traits::r_vector_c
ache<14>::update(Rcpp::Vector<14> const&)]+0x15): undefined reference to `double
* Rcpp::internal::r_vector_start<14, double>(SEXPREC*)'
collect2: ld returned 1 exit status
Any ideas what I am missing?
Jim is correct in his earlier answer, but there is more.
When using RInside , you also need to
include and link with Rcpp (which RInside depends upon) a
include and link with R (which both depends upon) as well as of course
RInside's own library
The easiest way of doing this is to simply use the Makefile from the examples/standard directory --- given that you copied the code of one of the examples, you should also copy the build instructions.
Lastly, and that is your biggest issue: RInside applications do not currently work on Windows, which is clearly documented on the RInside page. It will build, but segfault on startup. Debugging help would be appreciated, this works on OS X and Linux.
You're not actually linking in the R library (whatever that is).