I found this code in a tutorial
http://www.penguinprogrammer.co.uk/c-beginners-tutorial/introduction/
// This line is necessary to be able to output information to the screen
#include <iostream>
// The program starts here and carries on line by line
int main(){
// Create two integers a and b containing 10 and 5
int a = 10;
int b = 5;
/* Add them together and store the result in another
integer called sum */
int sum = a + b;
// Output the sum to the screen
std::cout << sum << std::endl;
// End the program and send a value of 0 (success) back
// to the operating system
return 0;
}
I want to compile it
Have installed clang by doing
apt-get install clang
Compiling by doing
clang -x c++ tutorial.cpp
error
/tmp/tutorial-aa5f7a.o: In function `main':
tutorial.cpp:(.text+0xa): undefined reference to `std::cout'
tutorial.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(int)'
tutorial.cpp:(.text+0x3a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
tutorial.cpp:(.text+0x46): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/tutorial-aa5f7a.o: In function `__cxx_global_var_init':
tutorial.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()'
tutorial.cpp:(.text.startup+0x19): undefined reference to `std::ios_base::Init::~Init()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Use clang++ tutorial.cpp - the -x c++ is useful if you only want to compile the source file, using -c, but if you are also linking the application into an executable, you want clang to know that you are linking a C++ application, and add the c++ libraries to the link command (if you want to see what clang actually does, add the -v option.
Related
a.cpp:
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}
cmd input:
arm-none-eabi-g++ -specs=nosys.specs a.cpp
cmd output:
/usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/lib/thumb/nofp/libstdc++.a(random.o): in function `std::(anonymous namespace)::__libc_getentropy(void*)':
/build/arm-none-eabi-gcc/src/gcc-12.2.0/libstdc++-v3/src/c++11/random.cc:179: undefined reference to `getentropy'
/usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/lib/thumb/nofp/libstdc++.a(random.o): in function `std::random_device::_M_init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/build/arm-none-eabi-gcc/src/gcc-12.2.0/libstdc++-v3/src/c++11/random.cc:452: undefined reference to `getentropy'
/usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/12.2.0/../../../../arm-none-eabi/lib/thumb/nofp/libc.a(lib_a-arc4random.o): in function `_rs_stir':
/build/arm-none-eabi-newlib/src/build-newlib/arm-none-eabi/thumb/nofp/newlib/libc/stdlib/../../../../../../../newlib-4.2.0.20211231/newlib/libc/stdlib/arc4random.c:89: undefined reference to `getentropy'
collect2: error: ld returned 1 exit status
I don't know why arm-none-eabi-g++ compilation fails, if I use arm-none-eabi-gcc to compile C language, it can be compiled successfully. Is there some missing parameter that is causing the compilation to fail?
I had a look at the official GCC Bug List where this problem is mentioned but it is marked as resolved.
GCC Bug Description
This question already has an answer here:
Undefined reference to `std::chrono::_V2::system_clock::now()' when linking with gfortran
(1 answer)
Closed 1 year ago.
I am trying to pass the array between functions of c++ and fortran. The Fortran code looks like:
PROGRAM vector_adder
IMPLICIT NONE
INTEGER,allocatable :: a(:),b(:),c(:)
INTEGER :: i, num
num = 6
allocate(a(num),b(num),c(num))
!C fill vectors with values
DO i = 1,num
a(i) = i*i
b(i) = i
END DO
CALL ADD_VECTORS(a,b,c,num)
WRITE(*,'(I5,I5,I5)') (c(i),i =1,num)
STOP
END PROGRAM
And the C++ code looks like:
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
void add_vectors__(int* a, int* b, int* c, int& num);
extern "C" void add_vectors_(int* a, int* b, int* c, int& num)
{
add_vectors__(a, b, c, num);
}
void add_vectors__(int* a, int* b, int* c, int& num)
{
for (size_t i = 0; i<num; i++)
{
c[i] = a[i] + b[i];
cout <<c[i]<<endl;
}
}
And I compile the code as:
rm -r *.o
rm -r prog
g++ -c c++Code.cpp
gfortran -c fCode.f90
gfortran -o prog fCode.o c++Code.o
./prog
If I remove the line of court in the c++ file, the code runs. But introducing the cout line gives me the following errors of undefined references as:
c++Code.o: In function `add_vectors__(int*, int*, int*, int&)':
c++Code.cpp:(.text+0xc0): undefined reference to `std::cout'
c++Code.cpp:(.text+0xc5): undefined reference to `std::ostream::operator<<(int)'
c++Code.cpp:(.text+0xcf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
c++Code.cpp:(.text+0xda): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
c++Code.o: In function `__static_initialization_and_destruction_0(int, int)':
c++Code.cpp:(.text+0x110): undefined reference to `std::ios_base::Init::Init()'
c++Code.cpp:(.text+0x125): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
./run: line 6: ./prog: No such file or directory
The functions listed are from the C++ runtime library libstdc++. Since you're using gfortran to build the final library, it will not link in libstdc++ unless you explicitly tell gfortran to do so.
Using g++ or gfortran to link .o files is a convenient shorthand. The real linker is ld; the other commands just tell ld what their respective libraries are.
I'm trying to call Matlab function from C++ (for NS-3) in Eclipse IDE on Ubuntu 18.04. I used Matlab Library Compiler to compile the .m file into a C++ Shared Library. How do I call the function correctly?
The corresponding MATLAB Runtime has been installed and configured. At first it can't find "mclmcrrt.h", I copied the /usr/local/MATLAB/R2018b/extern/include folder and changed the library header to include "./include/mclmcrrt.h". Then the IDE can find the header files correctly.
A C++ script to test the library as follows:
/* C = A+B */
#include <iostream>
#include "libtestAdd.h"
using namespace std;
int main(){
double A[] = {1};
double B[] = {2};
int nargout = 1;
mwArray input_A (1,1,mxDOUBLE_CLASS,mxREAL);
input_A.SetData(M,1);
mwArray input_B (1,1,mxDOUBLE_CLASS,mxREAL);
input_B.SetData(K,1);
mwArray C (1,1,mxDOUBLE_CLASS,mxREAL);
if (!mclInitializeApplication(NULL,0)){
std::cerr << "Could not initialize the application properly."
<< std::endl;
return -1;
}
libtestAddInitialize();
testAdd(nargout, C, input_A, input_B);
libtestAddTerminate();
return 1;
}
The Matlab function is C=A+B.
The .m file has been compiled into a C++ Shared Library ("libtestAdd.so" and "libtestAdd.h"). The C++ script try to call the "testAdd" function from the library.
Matlab Runtime has been correctly installed. /etc/profile has been changed to export the follows path to LD_LIBRARY_PATH environment variable:
/usr/local/MATLAB/MATLAB_Runtime/v95/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v95/sys/os/glnxa64
The error messages as follows:
~/Desktop/libtestAdd/for_testing$ cd "/home/csrl/Desktop/libtestAdd/for_testing/" && g++ main.cpp -o main && "/home/csrl/Desktop/libtestAdd/for_testing/"main
/tmp/ccyFn7iq.o: In function `main':
main.cpp:(.text+0xde): undefined reference to `libtestAddInitialize'
main.cpp:(.text+0xf4): undefined reference to `testAdd(int, mwArray&, mwArray const&, mwArray const&)'
main.cpp:(.text+0xf9): undefined reference to `libtestAddTerminate'
/tmp/ccyFn7iq.o: In function `mwException::mwException()':
main.cpp:(.text._ZN11mwExceptionC2Ev[_ZN11mwExceptionC5Ev]+0x46): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::mwException(char const*)':
main.cpp:(.text._ZN11mwExceptionC2EPKc[_ZN11mwExceptionC5EPKc]+0x5a): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::mwException(error_info*, bool)':
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x61): undefined reference to `ref_count_obj_addref_proxy'
main.cpp:(.text._ZN11mwExceptionC2EP10error_infob[_ZN11mwExceptionC5EP10error_infob]+0x7a): undefined reference to `mclcppCreateError_proxy'
/tmp/ccyFn7iq.o: In function `mwException::~mwException()':
main.cpp:(.text._ZN11mwExceptionD2Ev[_ZN11mwExceptionD5Ev]+0x36): undefined reference to `ref_count_obj_release_proxy'
/tmp/ccyFn7iq.o: In function `mwException::what() const':
main.cpp:(.text._ZNK11mwException4whatEv[_ZNK11mwException4whatEv]+0x25): undefined reference to `error_info_get_message_proxy'
/tmp/ccyFn7iq.o: In function `mwException::raise_error()':
main.cpp:(.text._ZN11mwException11raise_errorEv[_ZN11mwException11raise_errorEv]+0x2a): undefined reference to `mclcppGetLastError_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::mwArray(unsigned long, unsigned long, mxClassID, mxComplexity)':
main.cpp:(.text._ZN7mwArrayC2Emm9mxClassID12mxComplexity[_ZN7mwArrayC5Emm9mxClassID12mxComplexity]+0x52): undefined reference to `mclGetMatrix_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::~mwArray()':
main.cpp:(.text._ZN7mwArrayD2Ev[_ZN7mwArrayD5Ev]+0x26): undefined reference to `ref_count_obj_release_proxy'
/tmp/ccyFn7iq.o: In function `mwArray::SetData(double*, unsigned long)':
main.cpp:(.text._ZN7mwArray7SetDataEPdm[_ZN7mwArray7SetDataEPdm]+0x2b): undefined reference to `array_ref_set_numeric_mxDouble_proxy'
collect2: error: ld returned 1 exit status
You need to link with the .so file. Add it to the end of your compile command.
I am trying to get OGDF working to see if it is suitable for my project, but I am having trouble with a sample program.
I am trying to compile this example program:
#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
using namespace ogdf;
int main()
{
Graph G;
randomSimpleGraph(G, 10, 20);
DfsAcyclicSubgraph DAS;
DAS.callAndReverse(G);
G.writeGML("test.gml");
return 0;
}
using this command:
$g++ -pthread -I ./OGDF/ -L ./OGDF/_release/ -lOGDF test2.cpp
But I get the following error
/tmp/ccbpkfdt.o: In function `main':
test2.cpp:(.text+0x12): undefined reference to `ogdf::Graph::Graph()'
test2.cpp:(.text+0x2e): undefined reference to `ogdf::randomSimpleGraph(ogdf::Graph&, int, int)'
test2.cpp:(.text+0x4e): undefined reference to `ogdf::AcyclicSubgraphModule::callAndReverse(ogdf::Graph&)'
test2.cpp:(.text+0x62): undefined reference to `ogdf::Graph::writeGML(char const*) const'
test2.cpp:(.text+0x7f): undefined reference to `ogdf::Graph::~Graph()'
test2.cpp:(.text+0xa1): undefined reference to `ogdf::Graph::~Graph()'
/tmp/ccbpkfdt.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0xfb): undefined reference to `ogdf::Initialization::Initialization()'
test2.cpp:(.text+0x112): undefined reference to `ogdf::Initialization::~Initialization()'
/tmp/ccbpkfdt.o: In function `ogdf::DfsAcyclicSubgraph::DfsAcyclicSubgraph()':
test2.cpp:(.text._ZN4ogdf18DfsAcyclicSubgraphC2Ev[_ZN4ogdf18DfsAcyclicSubgraphC5Ev]+0x16): undefined reference to `vtable for ogdf::DfsAcyclicSubgraph'
/tmp/ccbpkfdt.o: In function `ogdf::DfsAcyclicSubgraph::~DfsAcyclicSubgraph()':
test2.cpp:(.text._ZN4ogdf18DfsAcyclicSubgraphD2Ev[_ZN4ogdf18DfsAcyclicSubgraphD5Ev]+0xb): undefined reference to `vtable for ogdf::DfsAcyclicSubgraph'
collect2: error: ld returned 1 exit status
I tried compiling hello world, with an include from OGDF, and I still got:
undefined reference to `ogdf::Initialization::Initialization()'
I think I am not linking properly or something?
You have to be very careful in which order you type stuff when linking with a library.
Try putting test2.cpp before -lOGDF instead, like this:
g++ -pthread -I ./OGDF/ -L ./OGDF/_release/ test2.cpp -lOGDF
You must build your program using the -DOGDF_DLL when using OGDF as a shared library.
See here: http://www.ogdf.net/doku.php/tech:defines
I am a leda-6.3 library user.
I used graphwin to manipulate and display graphs, but I found that references
to graphwin methods are undefined while compiling the code although they
are declared in the LEDA/incl/LEDA/graphics/graphwin.h
So I think it is a problem of object file.
#include <LEDA/graphics/graphwin.h>
#include <LEDA/graph/graph_alg.h>
using namespace leda;
int main()
{
GraphWin gw("LEDA Graph Editor");
node u=gw.new_node(point(100,100));
node v=gw.new_node(point(100,200));
gw.new_edge(u,v);
gw.display();
gw.get_window().read_mouse();
graph& G=gw.get_graph();
G.new_node();
gw.get_window().read_mouse();
gw.update_graph();
gw.get_window().read_mouse();
return 0;
}
compilation: g++ -I$LEDAROOT/incl -L$LEDAROOT gw.cpp -lleda -lX11 -lm -o gw
ERROR :
/tmp/ccVHyRbL.o: In function `main':
gw.cpp:(.text+0x1e): undefined reference to `leda::GraphWin::GraphWin(char const*)'
gw.cpp:(.text+0x58): undefined reference to `leda::GraphWin::new_node(leda::point const&)'
gw.cpp:(.text+0xc6): undefined reference to `leda::GraphWin::new_node(leda::point const&)'
gw.cpp:(.text+0x11c): undefined reference to `leda::GraphWin::new_edge(leda::node_struct*, leda::node_struct*)'
gw.cpp:(.text+0x128): undefined reference to `leda::GraphWin::display()'
gw.cpp:(.text+0x17e): undefined reference to `leda::GraphWin::update_graph()'
collect2: ld returned 1 exit status
Which edition of LEDA are you using?
Please consider that free edition of LEDA does not contain GraphWin.
So, it dos not contain GraphWin libraries, which results to getting such errors while compiling your program.