compiling .cpp with gcc - c++

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++

Related

Undefined reference to boost::system::generic_category() although libs are given to g++

What are the linker flags I'm missing here?
I try to compile this on an Ubuntu 18.04 LTS and it fails, it works inside a Debian 9 docker image:
#include <boost/dll.hpp>
// Trying to compile it with:
// g++ -o program -lboost_filesystem -ldl -lboost_system program.cpp
int main() {
boost::dll::program_location();
return 0;
}
The error I get is:
/tmp/ccKlWUUd.o: In function `__static_initialization_and_destruction_0(int, int)':
program.cpp:(.text+0x68): undefined reference to `boost::system::generic_category()'
program.cpp:(.text+0x74): undefined reference to `boost::system::generic_category()'
program.cpp:(.text+0x80): undefined reference to `boost::system::system_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_code::error_code()':
program.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xb8): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xf3): undefined reference to `boost::system::generic_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xb8): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xf3): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0x1d2): undefined reference to `boost::system::generic_category()'
/tmp/ccKlWUUd.o: In function `boost::dll::detail::report_error(boost::system::error_code const&, char const*)':
program.cpp:(.text._ZN5boost3dll6detail12report_errorERKNS_6system10error_codeEPKc[_ZN5boost3dll6detail12report_errorERKNS_6system10error_codeEPKc]+0x2a): undefined reference to `dlerror'
/tmp/ccKlWUUd.o: In function `boost::filesystem::read_symlink(boost::filesystem::path const&, boost::system::error_code&)':
program.cpp:(.text._ZN5boost10filesystem12read_symlinkERKNS0_4pathERNS_6system10error_codeE[_ZN5boost10filesystem12read_symlinkERKNS0_4pathERNS_6system10error_codeE]+0x36): undefined reference to `boost::filesystem::detail::read_symlink(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status
The system here is:
gcc: 7.0.3
boost: 1.65.1
libc6: 2.73
Your build command is in the wrong order, and order matters.
GCC reads left-to-right, taking symbols from libraries when it already knows it needs them. As you put program.cpp last, you don't make that known until all listed libraries have already been identified and discarded.
Put program.cpp first, then the libraries it needs.
g++ -o program program.cpp -lboost_filesystem -ldl -lboost_system
Yes, it's kind of weird. (Even weirder that it worked on Debian! Though apparently only some "recent" Linuxy distributions default --as-needed on, which is what causes the behaviour you see, showing that the behaviour isn't necessarily guaranteed. Perhaps Debian 9 just outright does not do that.)
More info:
Why does the order in which libraries are linked sometimes cause errors in GCC?

Codeblocks C++ Compiling Error on Codeblocks w/ GCC

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.

Compiling and linking C code invoking C++ function

I have a problem when invoking a C++ method from my C code. The method I need to invoke in the C++ code is not within a class. I am trying to setup a simple example and I have the following files:
//header.h
#ifdef __cplusplus
#include <iostream>
extern "C" {
#endif
int print(int i, double d);
#ifdef __cplusplus
}
#endif
//ccode.c
#include "header.h"
main() {
printf("hello");
print(2,2.3);
}
//cppcode.cc
#include "header.h"
using namespace std;
int print(int i, double d)
{
cout << "i = " << i << ", d = " << d;
}
Probably my error is in the way I am trying to compile and link this. I am doing the following:
g++ -c cppcode.cc -o cppcode.o
That goes fine.
gcc ccode.c cppcode.o -o ccode
Here I get the following errors:
cppcode.o: In function `print':
cppcode.cc:(.text+0x16): undefined reference to `std::cout'
cppcode.cc:(.text+0x1b): 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*)'
cppcode.cc:(.text+0x28): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
cppcode.cc:(.text+0x35): 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*)'
cppcode.cc:(.text+0x42): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
cppcode.o: In function `__static_initialization_and_destruction_0(int, int)':
cppcode.cc:(.text+0x6b): undefined reference to `std::ios_base::Init::Init()'
cppcode.cc:(.text+0x70): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status
I assume that this happens because I am using the C compiler. What is the correct way to compile and link this small example?
The idea is that I have the C code running and just invoke the C++ functions, without having to rewrite them in C. Thanks in advance for help!
I am using Ubuntu 12.04, gcc version 4.6.3
You need to link C++ runtime library.
gcc ccode.c cppcode.o -o ccode -lstdc++
You should compile and link separately. Use g++ to link in order to get the proper standard library.
g++ -c cppcode.cc -o cppcode.o
gcc -c ccode.c -o ccode.o
g++ ccode.o cppcode.o -o ccode
The g++ compiler automatically links your program with the standard cpp library.
When you compile with gcc, the linker can find reference to that.
You have two options.
One is to compile the c file with g++. Second is to force the linkage of the standard cpp library.
Here's whats written in gcc guide:
-static-libstdc++
When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.
Run the following command:
gcc ccode.c cppcode.o -o ccode -lstdc++

How does gcc link static library?

I have a simple program use static library for control pci device. I have got some examples. But i want to make myself some examples but i can't link static libraries.
I have 3 file: led.cpp main.cpp main.h
gcc -c led.cpp -I../utils -I../driver -o led.o
gcc -c main.cpp -I../utils -I../driver -o main.o
it' s ok. Succesfuly creating main.o and led.o object files.
But when linking state, its broken. 24dsi20c500k_utils.a and 24dsi20c500k_dsl.a static libraries.
gcc led.o main.o ../utils/24dsi20c500k_utils.a ../docsrc/24dsi20c500k_dsl.a -o led
Output is shown:
led.o: In function `led_tests(int)':
led.cpp:(.text+0x18): undefined reference to `gsc_label(char const*)'
led.cpp:(.text+0x31): undefined reference to `dsi20c500k_initialize(int, int)'
led.cpp:(.text+0x39): undefined reference to `gsc_label_level_inc()'
led.cpp:(.text+0x5b): undefined reference to `dsi20c500k_led(int, int, int, int*)'
led.cpp:(.text+0x81): undefined reference to `dsi20c500k_led(int, int, int, int*)'
led.cpp:(.text+0xa2): undefined reference to `gsc_label_level_dec()'
led.cpp:(.text+0xb1): undefined reference to `dsi20c500k_initialize(int, int)'
main.o: In function `_perform_tests(int)':
main.cpp:(.text+0x3a1): undefined reference to `gsc_label(char const*)'
main.cpp:(.text+0x3c6): undefined reference to `gsc_id_driver(int, char const*)'
main.o: In function `main':
main.cpp:(.text+0x42c): undefined reference to `gsc_label_init(int)'
main.cpp:(.text+0x49a): undefined reference to `gsc_id_host()'
main.cpp:(.text+0x4a4): undefined reference to `gsc_count_boards(char const*)'
main.cpp:(.text+0x4b6): undefined reference to `gsc_select_1_board(int, int*)'
main.cpp:(.text+0x4d7): undefined reference to `gsc_label(char const*)'
main.cpp:(.text+0x500): undefined reference to `gsc_dev_open(unsigned int, char const*)'
main.cpp:(.text+0x54f): undefined reference to `gsc_dev_close(unsigned int, int)'
collect2: ld returned 1 exit status
make: *** [led] Error 1
if i rename cpp files to c files compilation succesfull. What is the problem?
gcc -c main.cpp will compile your code as C++ code.
What likely happens then, is that your main.cpp includes other header files, that are not meant to be compiled as C++. This means that when including these header files in a C++ program , gcc will assume all the stuff the header files declares are C++.
And if they're not, you get linker errors if you try to link to C code that the compiler assumed was C++.
You can remedy this by stating that those header files are C. e.g. in your main.cpp you'd do
extern "C" {
#include "some_c_lib.h"
}
You probably didn't use extern "C".
if i rename cpp files to c files compilation succesfull. What is the
problem?

gnuplot-iostream not linking to boost

Hi I'm trying to use gnuplot-iostream, at the minute I'm just trying to get the code here working. When I try to combile I get errors in the linker saying:
In function `stream<int, boost::iostreams::file_descriptor_flags>':
/usr/include/boost/iostreams/stream.hpp:130: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
In function `boost::iostreams::file_descriptor_sink boost::iostreams::detail::wrap<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink const&, boost::disable_if<boost::iostreams::is_std_io<boost::iostreams::file_descriptor_sink>, void>::type*)':
/usr/include/boost/iostreams/detail/wrap_unwrap.hpp:53: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
In function `concept_adapter':
/usr/include/boost/iostreams/detail/adapter/concept_adapter.hpp:67: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/usr/include/boost/iostreams/detail/adapter/concept_adapter.hpp:38: undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
In function `long boost::iostreams::detail::write_device_impl<boost::iostreams::output>::write<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, boost::iostreams::char_type_of<boost::iostreams::file_descriptor_sink>::type const*, long)':
/usr/include/boost/iostreams/write.hpp:121: undefined reference to `boost::iostreams::file_descriptor::write(char const*, long)'
In function `void boost::iostreams::detail::close_impl<boost::iostreams::closable_tag>::close<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, std::_Ios_Openmode)':
/usr/include/boost/iostreams/close.hpp:224: undefined reference to `boost::iostreams::file_descriptor::close()'
In function `std::fpos<__mbstate_t> boost::iostreams::detail::seek_device_impl<boost::iostreams::any_tag>::seek<boost::iostreams::file_descriptor_sink>(boost::iostreams::file_descriptor_sink&, long, std::_Ios_Seekdir, std::_Ios_Openmode)':
/usr/include/boost/iostreams/seek.hpp:137: undefined reference to `boost::iostreams::file_descriptor::seek(long, std::_Ios_Seekdir)'
collect2: ld returned 1 exit status
Note I have boost installed and have previously compiled programs that use iostream.
Any help massively appreciated. Thanks
Ruling out the obvious... are you compiling with g++ -lboost_iostreams? If you can share some information about how the compiler and linker are being invoked it will make it easier to answer your question.
Run into exact problem when tried to use g++ with gnuplot-iostream library.
g++ prog.cpp -L/usr/lib -lboost_filesystem -lboost_system -lboost_iostreams
Resolved the issue for me - the code were compiled successfully with no errors.