I have the following make file
g++ -Wall -O3 -g -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 Matrix.cc -L /usr/lib64/libcblas.so.0 util.cc word_io.cc net_lbl_2reps_scalable.cc train_lbl_2r_ptb.cc -o train_lbl_2r_ptb
However I get the error
/tmp/cc9NLGFL.o: In function Matrix::scaleAddAB(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:316: undefined reference tocblas_sgemm'
/tmp/cc9NLGFL.o: In function Matrix::scaleAddAtransB(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:330: undefined reference tocblas_sgemm'
/tmp/cc9NLGFL.o: In function Matrix::scaleAddABtrans(Matrix const&, Matrix const&, float, float)':
/home/ncelm/Matrix.cc:344: undefined reference tocblas_sgemm'
The function due to which the error is occuring:
void Matrix::scaleAddABtrans(const Matrix &A, const Matrix &B, float targetScale, float prodScale)
{
assert(A.rows() == rows() && A.cols() == B.cols() && B.rows() == cols());
::cblas_sgemm(CblasColMajor, CblasNoTrans, CblasTrans,
A.rows(), B.rows(), A.cols(),
prodScale, // Scale the product by 1
A.data(), A.rows(),
B.data(), B.rows(),
targetScale, // Scale the target by this before adding the product matrix
data(), rows());
}
It is able to link the file but not find the sgemm. Unable to understand why?
As user6292850 notes, the -L option takes a directory name, not a library name. To name the library, use -lcblas. You probably don't need to use -L in this case, because /usr/lib64 is likely on the default search path.
One other bit of advice: Put the linker options and the library names after any source and object filenames on the command line. In make it would conventionally look something like this:
$ c++ $(CXXFLAGS) -o train_lbl_2r_ptb $(SRC) $(LDFLAGS) -lcblas
You do that because the linker works its way down the line, as it were, to resolve names. If, in your example, util.cc uses a cblas function, the linker might not find it unless the library appears to the right on the command line.
Related
I am trying to compile the latest version of QEmacs (a tiny version of Emacs):
https://github.com/dmacvicar/qemacs
Everything is OK, I have the needed libraries, including Qt, but in the linking phase, the linker gives errors undefined reference to vtable. I looked at several questions and answers on stackoverflow, and I checked and verified that all three virtual methods have appropriate blank implementations available in qt.cpp. So, what's the problem?
gcc -g -Wl,-E -o qe_g .objs/qe.o .objs/parser.o .objs/charset.o .objs/buffer.o .objs/input.o .objs/display.o .objs/util.o .objs/hex.o .objs/list.o .objs/cutils.o .objs/extras.o .objs/variables.o .objs/qt.o .objs/tty.o .objs/kmap.o .objs/unicode_join.o .objs/arabic.o .objs/indic.o .objs/qfribidi.o .objs/charsetjis.o .objs/charsetmore.o .objs/unihex.o .objs/bufed.o .objs/clang.o .objs/xml.o .objs/htmlsrc.o .objs/lisp.o .objs/makemode.o .objs/markdown.o .objs/orgmode.o .objs/perl.o .objs/script.o .objs/extra-modes.o .objs/shell.o .objs/dired.o .objs/latex-mode.o .objs/archive.o .objs/x11.o .objs/html.o .objs/docbook.o .objs/qeend.o libqhtml/libqhtml.a -ldl `pkg-config --libs Qt5Gui Qt5Core Qt5Widgets` -lstdc++ -lpthread -lm -lXv -L/usr/X11R6/lib -lXext -lX11 -L./libqhtml -lqhtml -DQT_NO_DEBUG_OUTPUT
.objs/qt.o: In function `QEQtView::~QEQtView()':
/home/ho1/projects/qemacs/qt.cpp:197: undefined reference to `vtable for QEQtView'
.objs/qt.o: In function `QEQtView::QEQtView(QEQtContext*, QWidget*)':
/home/ho1/projects/qemacs/qt.cpp:192: undefined reference to `vtable for QEQtView'
.objs/qt.o: In function `QEQtApplication::QEQtApplication()':
/home/ho1/projects/qemacs/qt.cpp:525: undefined reference to `vtable for QEQtApplication'
collect2: error: ld returned 1 exit status
make: *** [qe_g] Error 1
This is the implementation of the virtual methods:
# Line 192
QEQtView::QEQtView(QEQtContext *ctx, QWidget *parent)
: QWidget(parent),
_ctx(ctx),
_repaints(0)
{
//setAttribute(Qt::WA_OpaquePaintEvent);
}
# Line 197
QEQtView::~QEQtView()
{
}
....
# Line 525
QEQtApplication::QEQtApplication()
: QApplication(qe_state.argc, qe_state.argv)
{
}
This is the output of nm:
$ nm -C .objs/qt.o | grep QEQtView::
0000000000000850 T QEQtView::closeEvent(QCloseEvent*)
0000000000001340 T QEQtView::mouseEvent(QMouseEvent*)
0000000000001c90 T QEQtView::paintEvent(QPaintEvent*)
00000000000000e0 T QEQtView::wheelEvent(QWheelEvent*)
0000000000001c40 T QEQtView::resizeEvent(QResizeEvent*)
0000000000001310 T QEQtView::slotSetClip(int, int, int, int)
0000000000000f60 T QEQtView::slotDrawText(QFont const&, int, int, QString const&, QColor const&, bool)
0000000000000200 T QEQtView::keyPressEvent(QKeyEvent*)
0000000000000f30 T QEQtView::slotSetCursor(int, int, int, int)
00000000000014e0 T QEQtView::mouseMoveEvent(QMouseEvent*)
00000000000014c0 T QEQtView::mousePressEvent(QMouseEvent*)
00000000000014d0 T QEQtView::mouseReleaseEvent(QMouseEvent*)
0000000000001160 T QEQtView::slotFillRectangle(int, int, int, int, QColor const&, bool)
0000000000001bd0 T QEQtView::slotResizeDoubleBuffer(QSize const&)
00000000000012c0 T QEQtView::slotFlush()
0000000000000eb0 T QEQtView::QEQtView(QEQtContext*, QWidget*)
0000000000000eb0 T QEQtView::QEQtView(QEQtContext*, QWidget*)
00000000000000b0 T QEQtView::~QEQtView()
0000000000000080 T QEQtView::~QEQtView()
0000000000000080 T QEQtView::~QEQtView()
00000000000000d0 T non-virtual thunk to QEQtView::~QEQtView()
00000000000000a0 T non-virtual thunk to QEQtView::~QEQtView()
0000000000000000 r QEQtView::closeEvent(QCloseEvent*)::__PRETTY_FUNCTION__
$ nm -C .objs/qt.o | grep QEQtApplication::
00000000000014f0 T QEQtApplication::QEQtApplication()
00000000000014f0 T QEQtApplication::QEQtApplication()
The problem was the build process, and not the actual source code. I've built the project with cmake and it was fine.
This question already has answers here:
Linking files in g++
(4 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I am simply trying to use a function whose prototype is declared in a separate header file (ML_hash.h) and whose declaration is made in separate cpp file. I am trying to call this function in a different header file (HashNode.h). Here is the relevant code:
HashNode.h:
#ifndef HASHNODE_H
#define HASHNODE_H
#include "ML_hash.h"
template < typename T >
class HashNode
{
... // function prototype declarations
};
template< typename T >
void HashNode< T >::insert(int key, T* object)
{
...
int retVal = ML_hash(1, 3);
...
}
...
#endif
ML_hash.h:
#ifndef INC_ML_HASH
#define INC_ML_HASH
int ML_hash(int level, int key );
#endif
The error I am getting is:
g++ -o hash Hashtest.o:
Hashtest.o: In function `HashNode<int>::insert(int, int*)':
/home/adalal1/programs/class/final_project/HashNode.h:72: undefined reference to ' ML_hash(int, int)'
/home/adalal1/programs/class/final_project/HashNode.h:88: undefined reference to ` ML_hash(int, int)'
Hashtest.o: In function `HashNode<int>::explode()':
/home/adalal1/programs/class/final_project/HashNode.h:117: undefined reference to ` ML_hash(int, int)'
collect2: error: ld returned 1 exit status
What I don't understand is why the C++ compiler doesn't recognize the ML_hash function defined in ML_hash.cpp. I did include ML_hash.h in that cpp file. Could anyone provide insight into why this is happening?
EDIT:
I compile the code with a Makefile shown below:
C++ = g++
CFLAGS = -c -g
all: hash
hash: Hashtest.o
$(C++) -o hash Hashtest.o
clean:
rm -f *.o
%.o: %.cpp
$(C++) $(CFLAGS) $*.cpp
'ld returned 1 exit status' -> this is a linker error, not a compiler error.
Looks like you are running into this issue:
Linking files in g++
EDIT:
Either
g++ -o hash ML_hash.cpp HashNode.cpp HashTest.cpp
or
g++ -c ML_hash.cpp
g++ -c HashNode.cpp
g++ -c HashTest.cpp
g++ -o hash ML_hash.o HashNode.o HashTest.o
EDIT2 with OP edit:
I'm no makefile expert, but it looks like the 'hash:' target is just missing ML_hash.o and HashNode.cpp
hash: HashNode.o
ML_hash.o
Hashtest.o
$(C++) -o hash Hashtest.o ML_hash.o HashNode.o
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.
I'm attempting to write a program using a genetic algorithm which is a particular type of optimization algorithm. I found a free library for this task called "Evolutionary Objects" and implemented a very simple genetic algorithm instance. The program code, the build commands that netbeans implements, and finally the error messages that I receive are posted below in separate blocks. If you allow me your help, you'll see that something is going wrong with the cout function. When I searched the internet for similar difficulties, I found that people had been correcting the problem by simply using g++ rather than gcc yet I am already using g++, as you can see. Any help would be appreciated....
The program code is immediately below:
#include <stdexcept>
#include <iostream>
#include <eo>
#include <ga.h>
typedef eoBit<double> Indi;
double binary_value(const Indi & _indi)
{
double sum = 0;
for (unsigned i = 0; i < _indi.size(); i++)
sum += _indi[i];
return sum;
}
void main_function(int argc, char **argv)
{
const unsigned int SEED = 42; // seed for random number generator
const unsigned int T_SIZE = 3; // size for tournament selection
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
const unsigned int POP_SIZE = 100; // Size of population
const unsigned int MAX_GEN = 400; // Maximum number of generation before STOP
const float CROSS_RATE = 0.8; // Crossover rate
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
const float MUT_RATE = 1.0; // mutation rate
rng.reseed(SEED);
eoEvalFuncPtr<Indi> eval( binary_value );
eoPop<Indi> pop;
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
{
Indi v; // void individual, to be filled
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
{
bool r = rng.flip(); // new value, random in {0,1}
v.push_back(r); // append that random value to v
}
eval(v); // evaluate it
pop.push_back(v); // and put it in the population
}
pop.sort();
cout << "Initial Population" << endl;
cout << pop;
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
eo1PtBitXover<Indi> xover;
eoBitMutation<Indi> mutation(P_MUT_PER_BIT);
eoGenContinue<Indi> continuator(MAX_GEN);
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);
gga(pop);
pop.sort();
cout << "FINAL Population\n" << pop << endl;
}
int main(int argc, char **argv)
{
main_function(argc, argv);
return 1;
}
}
Netbeans shows me that it is doing this when attempting build:
make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
rm -f -r build/Debug
rm -f dist/Debug/GNU-Linux-x86/eo_ga
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
CLEAN SUCCESSFUL (total time: 94ms)
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/eo_ga
make[2]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -I../../EO/EO/eo/src -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/eo_ga build/Debug/GNU-Linux-x86/main.o -L../../EO/EO/eo/build/lib /home/gregemerson/EO/EO/eo/build/lib/libcma.a /home/gregemerson/EO/EO/eo/build/lib/libeoutils.a /home/gregemerson/EO/EO/eo/build/lib/libes.a /home/gregemerson/EO/EO/eo/build/lib/libga.a
The error messages are immediately below:
/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:99: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:149: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/eo_ga] Error 1
make[2]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make: *** [.build-impl] Error 2
Your response did correct for my cout(pop) problem. However, I'm now seeing similar errors in a headers for one of the static libraries that I'm using along with what looks to be related to one of the basic c++ libraries. I get the following. Does this make sense to you?
build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
The issue is (for example) here:
cout << pop;
The error,
/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to
`operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
Means that there is no basic function that knows how to push an eoPrintable object into an ostream.
Based on the link provided in the comments, this eoPop class has defined a few methods for printing: sortedPrintOn and printOn.
In order to use one of them to print to cout, you would do the following:
pop.printOn(cout); //or pop.printOn(std::cout) if you remove using namespace std;
pop.printSortedOn(cout);
Many peoples are experiencing this problem when they try to use EO C++ lib for first time. I also faced this problem and were able to find the solution. The problem is that the linker does not find the static libraries that have to be linked while building the EO programs. I am now able to successfully compile and use the first tutorial i.e. FirstBitGA.cpp & FirstRealGA.cpp with EO-1.3.1 on Ubunut 10.04 (LTS).
Note: I am writing the complete steps starting from installation, so be patient. Also replace the word 'username' with your user account.
<< Solution >>
Download EO Lib from http://sourceforge.net/projects/eodev/files/latest/download?source=files
Create a directory named EO; I create it in /home/username/ .
Copy the EO Zip file to /home/username/EO and uncompressed it. This will create another directory named 'eo' under /home/username/EO.
Now build the installation files by running the build script in /home/username/EO/eo/build_gcc_linux_release in terminal.
The build script in step-4 will create a directory named 'release' under /home/username/EO/eo. In the release directory the 'lib' folder contains the libraries that have to be linked while compiling any EO program.
Now two paths have to be provided to the compiler for successful compilation:
6.1. The path to include files as a command line argument to g++ i.e. -I /home/username/EO/eo/src < OR > If you are using Qt creator IDE then enter the line ' INCLUDEPATH += /home/username/EO/eo/src' in the .pro file of the probject.
6.2. The path to all link libraries with -L argument to g++ i.e.
-L/home/username/EO/eo/release/lib/libcma.a /home/username/EO/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial/home/username/EO/eo/release/lib/libeoutils.a /home/username/EO/eo/release/lib/libes.a /home/username/EO/eo/release/lib/libga.a
For Qt Creator users, enter the following lines into .pro file,
LIBS += -L/home/username/EO/eo/release/lib/libcma.a /home/username/EO/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial.a /home/username/EO/eo/release/lib/libeoutils.a /home/username/EO/eo/release/lib/libes.a /home/username/EO/eo/release/lib/libga.a
Enjoy
I'm compiling and linking a cpp file against a pre-compiled library, and I'm getting an "undefined reference" error.
Firstly, this is the command (the library in question is quicknet3, the program I'm compiling is trapper):
g++ -w -g -I. -g -O3 -pipe -Wall -I/home/install/x86_64/include/quicknet3 -L/home/install/x86_64/lib -lquicknet3 -lintvec -lfltvec -o trapper trapper.cpp CMyException.cpp
Here's the undefined reference error:
/tmp/ccFuVczF.o: In function 'main':
trapper.cpp:1731: undefined reference to 'QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int, char const*, _IO_FILE*, int)'
The call in trapper.cpp (line 1731) is:
IN_PFILE = new QN_InFtrLabStream_PFile(0, "", fp, 1);
where fp is a FILE *, assigned as the result of an fopen call beforehand.
The constructor being called is defined in the relevant header file (QN_Pfile.h), as follows:
class QN_InFtrLabStream_PFile : public
QN_InFtrLabStream
{
public:
QN_InFtrLabStream_PFile(int a_debug, const char* a_dbgname, FILE* a_file, int a_indexed);
(... other declarations ...)
}
The definition of the constructor is indeed given in QN_Pfile.cc:
QN_InFtrLabStream_PFile::QN_InFtrLabStream_PFile(int a_debug,const char* a_dbgname, FILE* a_file, int a_indexed) : log(a_debug, "QN_InFtrLabStream_PFile", a_dbgname),file(a_file),indexed(a_indexed),buffer(NULL),sentind(NULL)
{
(... the usual constructor stuff :P ...)
}
I compiled the quicknet3 library myself, without error, and installed it to /home/install/x86_64/lib/libquicknet3.a
So, I can't understand why the call from trapper.cpp is unable to find the reference to this constructor definition. The g++ arguments of -L/home/install/x86_64/lib -lquicknet3 should do the trick, right?
Any ideas?
Thanks,
Roy
I notice that you're mixing FILE* and _IO_FILE*. I'm not familiar with the latter, are you sure they're one and the same?
A quick workaround is to add /home/install/x86_64/lib/libquicknet3.a to g++ commandline.
I you want to investigate further, if g++ is picking another copy of libquicknet3, you can pass -v to g++ so it will output its searching paths.
FILE is a typedef of _IO_FILE. Your linker is treating it as a unique type.
You could try:
IN_PFILE = new QN_InFtrLabStream_PFile(0, "", (FILE *)fp, 1);
to see if this resolve your constructor.
(FILE is defined in stdio.h, _IO_FILE in libio.h if you're interested)