I have written a C++ file in nano text editor (UNIX command line) and I am trying to execute it with this command: c++ main.cpp. When I run it, no error messages display, however, there's also no successful messages; simply nothing appears... I also tried clang which gave me some more details, click here to view the error message.
My code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
return 0;
}
Clang Output:
/usr/bin/ld: /tmp/main-ad7332.o: in function `main':
main.cpp:(.text+0x11): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text+0x24): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: main.cpp:(.text+0x2d): 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> >&)'
/usr/bin/ld: main.cpp:(.text+0x36): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/main-ad7332.o: in function `__cxx_global_var_init':
main.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.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)
No output whatsoever is the expected outcome. Running c++ only compiles the provided source file. To run your program, you will have to explicitly run the produced executable, which by default is a.out.
With regards to the error messages you received from clang, make sure that you are using clang++ and remembered to include the iostream header, which declares std::cout.
Related
This question already has answers here:
GCC linker can't find standard library?
(4 answers)
Closed 3 years ago.
The problem is compounded by the fact that I am far from programming.
I need to use the TPC-E test. I downloaded the source codes from their website.
There is a miserable and almost meaningless ReadMe.
Here are its contents:
To use the Makefile you must first set the following environment variables
to match your system and compiler requirements:
CXX: path and name of compiler
ex: export CXX=/opt/SUNWspro/bin/CC (C++ compiler on Solaris)
CCFLAGS: compiler options
ex: export CCFLAGS="-g -O"
-g enable debug
-O optimize code
LDFLAGS: linker/loader options
ex: export LDFLAGS="-g"
LIBS: extra libraries to link in
ex: export LIBS="-lpthread"
According to the instructions there, I created the necessary environment variables (I just removed the -g switch from the CCFLAGS variable). After that, I ran the make utility and got more than five thousand lines of errors. Apparently he lacks some libraries. On this my knowledge ends.
Here are some error messages received:
...
/usr/bin/ld: DateTime.o: in function `TPCE::CDateTime::Validate(int, int, int, int, int, int, int)': DateTime.cpp:(.text+0xee): undefined reference to `std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream(std::_Ios_Openmode)'
/usr/bin/ld: DateTime.cpp:(.text+0xfd): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: DateTime.cpp:(.text+0x107): undefined reference to `__cxa_allocate_exception'
/usr/bin/ld: DateTime.cpp:(.text+0x117): undefined reference to `std::__cxx11::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const'
/usr/bin/ld: DateTime.cpp:(.text+0x122): undefined reference to `std::out_of_range::out_of_range(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: DateTime.cpp:(.text+0x135): undefined reference to `operator delete(void*)'
...
/usr/bin/ld: error.cpp:(.text._ZN4TPCE10CSystemErrD0Ev[_ZN4TPCE10CSystemErrD5Ev]+0x24): undefined reference to `std::exception::~exception()'
/usr/bin/ld: error.cpp:(.text._ZN4TPCE10CSystemErrD0Ev[_ZN4TPCE10CSystemErrD5Ev]+0x31): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: error.o: in function `TPCE::CSystemErr::~CSystemErr()':error.cpp:(.text._ZN4TPCE10CSystemErrD2Ev[_ZN4TPCE10CSystemErrD5Ev]+0x1c): undefined reference to `operator delete(void*)'
...
/usr/bin/ld: locking.cpp:(.text+0x1fb): undefined reference to `operator delete(void*)'
/usr/bin/ld: locking.cpp:(.text+0x202): undefined reference to `std::runtime_error::~runtime_error()'
/usr/bin/ld: locking.cpp:(.text+0x209): undefined reference to `typeinfo for std::runtime_error'
/usr/bin/ld: locking.cpp:(.text+0x211): undefined reference to `__cxa_throw'
/usr/bin/ld: locking.o: in function `TPCE::CMutex::lock()':locking.cpp:(.text+0x246): undefined reference to `std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream(std::_Ios_Openmode)'
...
A search on the internet made me think of installing the libboost-all-dev package, but that didn't help. In the end, I gave up - I’m completely incomprehensible to the terminology and I don’t have time to study C ++ programming.
Operating System - Debian 10
Questions: which libraries need to be installed and is there anyone who used these tests, how to adapt them to PostgreSQL?
Full error log (892 230 bytes)
I replaced
export CXX=/usr/bin/gcc
with
export CXX=/usr/bin/c++
I have been writing c++ for a little while using Visual Studio on Windows, and I recently switched to Linux Mint, and installed Codeblocks to use as my C++ IDE. I have written a very simple program, which I cannot get to compile:
#include <iostream>
int main()
{
std::cout << "Hello world!" << endl;
std::cin.get();
return 0;
}
I have tried running this, and I get a console error message:
Executing: xterm -T Test -e /usr/bin/cb_console_runner LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. /home/***/workspaces/cpp-workspace/Test/bin/Debug/Test (in /home/***/workspaces/cpp-workspace/Test/.)
Process terminated with status -1 (0 minute(s), 0 second(s))
I have also tried running cc against my main.cpp file, and I get an error message saying:
/tmp/cc4RcEeE.o: In function `main':
main.cpp:(.text+0xe): undefined reference to `std::cout'
main.cpp:(.text+0x13): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text+0x1d): 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> >&)'
main.cpp:(.text+0x28): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.cpp:(.text+0x2f): undefined reference to `std::cin'
main.cpp:(.text+0x34): undefined reference to `std::istream::get()'
/tmp/cc4RcEeE.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x64): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x79): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Additional Information:
I have installed GCC on my machine
I installed Codeblocks using the Mint Software Manager
I have been researching this problem over the course of a week or so, and have been unable to get anything to work. Any help getting this compile would be much appreciated!
EDIT
I have checked through my compiler settings according to the suggestions given, and they all appear correct. I've added a screenshot of my settings in case someone sees something... Also, g++ is not listed as an option in the drop-down for compilers.
My codeblocks settings
cc builds C programs. That won't bring in the standard library for C++, hence the linker errors.
You want a C++ compiler, like the one you have already installed: g++.
Indeed, until you've built your program successfully, you won't be able to execute it.
This ought to be covered in your C++ book, which you can review now.
okay this I did accidentally , compiling .cpp using gcc and not g++
but I actually want to understand the console output, line by line, if it has any sense.
struct a{
int pointer;
int rollno;
};
struct a student,*studentref;
studentref = &student;
studentref->rollno = 141;
studentref->pointer = 8;
cout<<studentref->rollno<<") : "<<studentref->pointer<<endl;
compiling this code with gcc structpointers.cpp -o structp gives the following output:
sourab#sourab:/home/gbear/coding/learningds$ gcc structpointers.cpp -o structp
/tmp/ccXrq1Cv.o: In function `main':
structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'
structpointers.cpp:(.text+0x33): undefined reference to `std::ostream::operator<<(int)'
structpointers.cpp:(.text+0x38): 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> >&)'
structpointers.cpp:(.text+0x40): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccXrq1Cv.o: In function `__static_initialization_and_destruction_0(int, int)':
structpointers.cpp:(.text+0x6e): undefined reference to `std::ios_base::Init::Init()'
structpointers.cpp:(.text+0x7d): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
The most egregious difference between calling g++ and gcc on a .cpp file is that g++ automatically links in the C++ standard library, while gcc does not; all the errors you see are linker errors of missing references to stuff that is provided by the C++ standard library.
(notice that this isn't the only difference; see this question for details)
g++ and gcc are compiler drivers of GNU compiler collection, they have backends which are connected automatically once compilation mode in up.
The errors like "
structpointers.cpp:(.text+0x2e): undefined reference to `std::cout'
are link errors,since you have used gcc to compile cpp,the standard libraries specifically for cpp are not present, so it could not find syntax like std::cout etc.
you can also refer to : compiling with g++
I having trouble configuring distcc to compile C++ files. I did a standard small "Hello, World" program in C++ and am trying to get distcc to compile it locally (before I get fancy with a larger project), but I am getting "undefined reference" errors.
My program, called "hello.cpp":
#include <iostream>
int main(){
std::cout << "Hello World in c++" << std::endl;
return 0;
}
My command:
$ distcc hello.cpp
Terminal output:
/tmp/cc5rZwhV.o: In function `main': <br>
hello.cpp:(.text+0xa): undefined reference to `std::cout' <br>
hello.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' <br>
hello.cpp:(.text+0x14): 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> >&)' <br>
hello.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' <br>
/tmp/cc5rZwhV.o: In function `__static_initialization_and_destruction_0(int, int)': <br>
hello.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()' <br>
hello.cpp:(.text+0x59): undefined reference to `std::ios_base::Init::~Init()' <br>
collect2: error: ld returned 1 exit status <br>
distcc[21053] ERROR: compile hello.cpp on localhost failed <br>
Other information:
This program compiles just fine with g++ and C++. A similar program written in C using printf works with distcc.
Output of $ distcc --version:
distcc 3.2rc1 x86_64-unknown-linux-gnu
(protocols 1, 2 and 3) (default port 3632)
built Jul 7 2014 13:18:34
....(copyright stuff)
Add -lstdc++ to your linker command line (that is, add libstdc++ to your linked libraries explicitly, which normally g++ would do for you).
While you're at it, try icecc as well. I personally prefer it.
I'm trying the following command:
gcc -o test test.o -lblas
which gives the error:
/usr/bin/ld: test.o: undefined reference to symbol 'sqrtf##GLIBC_2.2.5'
/usr/bin/ld: note: 'sqrtf##GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try adding it to the linker command line
/lib64/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
However, when I add -lm at the end of the command, there is a whole bunch of error messages:
test.o: In function `main':
test.cpp:(.text+0xe9): undefined reference to `std::cout'
test.cpp:(.text+0xee): undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char,
std::char_traits<char> >&, char const*)'
[...]
collect2: error: ld returned 1 exit status
I have the same problem with you when I tried to link other libraries.
Here is my solutions:
install libtool;
add the library path to LD_LIBRARY_PATH;
I use root to make.
I don't know the exactly reason why it works on my Ubuntu. But I think you can try these steps.