This question already has answers here:
Programs compiles in g++ but exits with linker errors in gcc
(3 answers)
Closed 9 years ago.
I'm using Fedora 18 (with Gnome), I have installed gcc and gcc-c++, when I used gcc -o slowka.o slowka.cpp command I saw following errors :
slowka.cpp:(.text+0x1b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
slowka.cpp:(.text+0x8d): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
slowka.cpp:(.text+0xa0): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccp7fTFJ.o: In function `__static_initialization_and_destruction_0(int, int)':
slowka.cpp:(.text+0xdb): undefined reference to `std::ios_base::Init::Init()'
slowka.cpp:(.text+0xea): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccp7fTFJ.o: In function `bool std::operator==<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*)':
slowka.cpp:(.text._ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_EPKS3_[_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_EPKS3_]+0x1f): undefined reference to `std::string::compare(char const*) const'
/tmp/ccp7fTFJ.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I don't know what is the reason of it. My code :
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main()
{
printf("Witaj w aplikacji dodającej słówka ! Czy chcesz włączyć aplikację w tryb permanentny (t/N) ?\n");
string x;
scanf("%s", &x);
if(x != "t" && x != "T")
{
printf("Wybrano tryb \"jednego słówka\" !\n");
return 0;
}
return 0;
}
Normally, you'd use the C++ compiler to link a C++ program:
g++ -o slowka.o slowka.cpp
However, if you want an object file, you'd specify -c:
g++ -c -o slowka.o slowka.cpp
or perhaps:
gcc -c -o slowka.o slowka.cpp
(And the output name would inferred automatically by the compiler, so the -o slowka.o is optional.)
Related
This question already has answers here:
G++ Undefined Reference std::
(2 answers)
Closed 2 years ago.
I have a simple bit of code which is having trouble compiling.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
string hello = "Hello World";
printf("%s\n",hello.c_str());
return 0;
}
compiling with "gcc test.cpp -o test"
returns the following message.
/tmp/ccgbRaOf.o: In function main':
test.cpp:(.text+0x27): undefined reference tostd::allocator::allocator()'
test.cpp:(.text+0x3e): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
test.cpp:(.text+0x4a): undefined reference tostd::allocator::~allocator()'
test.cpp:(.text+0x56): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
test.cpp:(.text+0x6a): undefined reference tostd::__cxx11::basic_string, std::allocator >::~basic_string()'
test.cpp:(.text+0x8f): undefined reference to std::allocator<char>::~allocator()'
test.cpp:(.text+0xa9): undefined reference tostd::__cxx11::basic_string, std::allocator >::~basic_string()'
/tmp/ccgbRaOf.o: In function __static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0xe9): undefined reference tostd::ios_base::Init::Init()'
test.cpp:(.text+0xfe): undefined reference to std::ios_base::Init::~Init()'
/tmp/ccgbRaOf.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to__gxx_personality_v0'
collect2: error: ld returned 1 exit status
What am I doing wrong?
You compile it with gcc which is for C, but your code is C++. Use g++.
It does not work because you are using a C compiler. For C++ you need a different compiler. Try with g++
I have correctly installed crfsuite from source (https://github.com/downloads/chokkan/crfsuite/crfsuite-0.12.tar.gz).
But when I try to compile a very simple code, it seems that I have missed something.
Here is the code:
#include "crfsuite.hpp"
using namespace CRFSuite;
int main(int argc, char *argv[])
{
Tagger tagger;
}
Here's the command line to compile:
g++ -L/usr/local/lib -I/usr/local/include -lcrfsuite tagging.cpp
and the error:
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::Trainer()':
tagging.cpp:(.text+0x48): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::init()':
tagging.cpp:(.text+0x149): undefined reference to `crfsuite_create_instance'
tagging.cpp:(.text+0x1b7): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::clear()':
tagging.cpp:(.text+0x2dd): undefined reference to `crfsuite_data_finish'
tagging.cpp:(.text+0x2ed): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::append(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, int)':
tagging.cpp:(.text+0x495): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x51f): undefined reference to `crfsuite_item_init_n'
tagging.cpp:(.text+0x69b): undefined reference to `crfsuite_data_append'
tagging.cpp:(.text+0x6aa): undefined reference to `crfsuite_instance_finish'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::select(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x7e9): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x11a0): undefined reference to `crfsuite_create_instance_from_file'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::set(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&)':
tagging.cpp:(.text+0x16c6): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x1731): undefined reference to `crfsuite_item_init'
tagging.cpp:(.text+0x17e1): undefined reference to `crfsuite_attribute_set'
tagging.cpp:(.text+0x17f4): undefined reference to `crfsuite_item_append_attribute'
tagging.cpp:(.text+0x1854): undefined reference to `crfsuite_instance_finish'
tagging.cpp:(.text+0x18ac): undefined reference to `crfsuite_instance_finish'
collect2: error: ld returned 1 exit status
The paths are correct (/usr/local/lib, /usr/local/include)
The order of arguments matters. The library should go after your cpp file:
g++ -L/usr/local/lib -I/usr/local/include tagging.cpp -lcrfsuite
See the answer Why does the order in which libraries are linked sometimes cause errors in GCC? for more information.
My code won't compile and I don't understand the cryptic output from the compiler. Can you spot the error?
main.cpp
#include <vector>
#include <string>
int main() {
std::vector <std::string> myVar;
return 0;
}
Then in bash:
$ gcc main.cpp
/tmp/ccvmVBRq.o: In function `__gnu_cxx::new_allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::deallocate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, unsigned long)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m[_ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccvmVBRq.o: In function `void std::_Destroy<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
main.cpp:(.text._ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_[_ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_]+0x14): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccvmVBRq.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
This is C++ code, so you have to compile it with g++. Try:
$ g++ main.cpp
I am trying to compile this code in CodeBlocks
#include <boost/filesystem.hpp>
#include <iostream>
using namespace boost::filesystem;
int main()
{
if ( !boost::filesystem::exists( "myfile.txt" ) )
{
std::cout << "Can't find my file!" << std::endl;
}
}
With this compile flags:
g++.exe -Wall -fexceptions -g -O3 -pedantic-errors -Wall -std=c++0x -lboost_system -IC:\Users\moe\Desktop\boost_1_67_0 -c C:\Users\moe\Desktop\oo\main.cpp -o obj\Debug\main.o
But I always receive this error:
boost::system::generic_category()
this is the error log, that i receive when i compile the code:
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:733: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:736: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:748: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:703: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const&)'
Untitled4.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/operations.hpp:446: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2.exe: error: ld returned 1 exit status
Put the libraries at the end of the linker command line:
Why does the order of '-l' option in gcc matter?
Since your erro log also contained these:
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
It is clear that you also lack -lboost_filesystem.
So there is no use to just add -lboost_system to the g++ command.
You can add -lboost_system -lboost_filesystem to your g++ command.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string a;
cin>>a;
float v=strtof(a.c_str(),NULL);
cout<<v;
}
The above code works in g++ compiler. But in gcc its reporting following error.
/tmp/ccz60ueB.o: In function `main':
test1.cpp:(.text+0x12): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
test1.cpp:(.text+0x21): undefined reference to `std::cin'
test1.cpp:(.text+0x26): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
test1.cpp:(.text+0x32): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
test1.cpp:(.text+0x55): undefined reference to `std::cout'
test1.cpp:(.text+0x5a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(float)'
test1.cpp:(.text+0x66): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test1.cpp:(.text+0x81): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccz60ueB.o: In function `__static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0xac): undefined reference to `std::ios_base::Init::Init()'
test1.cpp:(.text+0xb1): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccz60ueB.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
You have to use g++ (or any C++ compiler) so it links with the C++ version of the library std. You shouldn't try to compile C++ code with a C compiler.
The following example works just fine for g++.
std::string asd="12312332434";
float as;
istringstream a;
a.str(asd);
a>>as;
cout<<as;
The following example must work fine for gcc.
//#include <stdio.h>
//#include <stdlib.h>
char asd[10];
float i;
fgets ( asd, 10, stdin );
i = atoi (asd);
gcc detects the language based on the file extension, so it can compile C++. However, you need to tell it to link against libstdc++.
For example:
gcc -o test test.cc -lstdc++
However, I'm not sure why you would do this.