string to float in gcc - c++

#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.

Related

trouble compiling code that includes string [duplicate]

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

Simple use of strings, returning strings from functions in C++

OK.
It's been a while since I wrote any C++.
And I'm rusty.
So what am I doing wrong here, and why?
#include <iostream>
std::string hello() {
return "another green world";
}
int main(int argc, char **argv) {
std::cout << hello() << std::endl;
return 0;
}
Then compiling with :
gcc test.cpp -o test
gives me
/tmp/ccxCCo47.o: In function `hello[abi:cxx11]()':
test.cpp:(.text+0x34): undefined reference to `std::allocator<char>::allocator()'
test.cpp:(.text+0x4b): 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+0x57): undefined reference to `std::allocator<char>::~allocator()'
test.cpp:(.text+0x7b): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccxCCo47.o: In function `main':
test.cpp:(.text+0xcb): undefined reference to `std::cout'
test.cpp:(.text+0xd0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0xda): 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> >&)'
test.cpp:(.text+0xe5): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0xf1): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x116): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccxCCo47.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x156): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x16b): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccxCCo47.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
You need to include your header file.
#include <iostream>
Also, try compiling with g++ instead of gcc.
g++ test.cpp

gcc won't compile C++ code

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

undefined reference to `EVP_sha3_256'

I complie my c++ program by input:
g++ main.cpp crypto/Sign.cpp -o main -lssl -lcrypto && ./main
Unfortunately, I got these output:
/tmp/ccEDu0Qv.o: In function `Sign::md_ctx_sign_init(SignatureScheme, evp_md_ctx_st*)':
Sign.cpp:(.text+0x1b): undefined reference to `EVP_MD_CTX_reset'
Sign.cpp:(.text+0x104): undefined reference to `EVP_sha3_224'
Sign.cpp:(.text+0x137): undefined reference to `EVP_sha3_256'
Sign.cpp:(.text+0x16a): undefined reference to `EVP_sha3_384'
Sign.cpp:(.text+0x196): undefined reference to `EVP_sha3_512'
/tmp/ccEDu0Qv.o: In function `Sign::md_ctx_veri_init(SignatureScheme, evp_md_ctx_st*)':
Sign.cpp:(.text+0x22f): undefined reference to `EVP_MD_CTX_reset'
Sign.cpp:(.text+0x318): undefined reference to `EVP_sha3_224'
Sign.cpp:(.text+0x34b): undefined reference to `EVP_sha3_256'
Sign.cpp:(.text+0x37e): undefined reference to `EVP_sha3_384'
Sign.cpp:(.text+0x3aa): undefined reference to `EVP_sha3_512'
/tmp/ccEDu0Qv.o: In function `Sign::sign_hash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, evp_md_ctx_st*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x4a9): undefined reference to `EVP_MD_CTX_free'
Sign.cpp:(.text+0x53f): undefined reference to `EVP_MD_CTX_free'
/tmp/ccEDu0Qv.o: In function `Sign::get_public_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x697): undefined reference to `EVP_PKEY_get0_EC_KEY'
/tmp/ccEDu0Qv.o: In function `Sign::get_private_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x85b): undefined reference to `EVP_PKEY_get0_EC_KEY'
/tmp/ccEDu0Qv.o: In function `Sign::EC_sign(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> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, SignatureScheme)':
Sign.cpp:(.text+0xa4a): undefined reference to `EVP_MD_CTX_new'
/tmp/ccEDu0Qv.o: In function `Sign::EC_veri(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> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, SignatureScheme)':
Sign.cpp:(.text+0xc56): undefined reference to `EVP_MD_CTX_new'
collect2: error: ld returned 1 exit status
So, what's wrong happend? How can I fix this problem?
The openssl I use is:
#:~$ which -a openssl
/usr/local/bin/openssl
/usr/bin/openssl
#:~$ openssl version
OpenSSL 1.1.1-pre5 (beta) 17 Apr 2018
In crypto/Sign.h, it include:
#include <openssl/conf.h>
#include <openssl/ec.h> // for EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_KEY_new, EC_KEY_set_group, EC_KEY_generate_key, EC_KEY_free
#include <openssl/ecdsa.h> // for ECDSA_do_sign, ECDSA_do_verify
#include <openssl/err.h>
#include <openssl/evp.h>
#include <iostream>
#include <string>

C++ errors while compiling

I am trying to compile a game, but getting 100+ errors like:
C:\Users\AppData\Local\Temp\cctQCagR.o: In function `load_image(std::string)':
main.cpp:(.text+0x4bd4): undefined reference to `std::string::c_str() const'
C:\Users\Bill\AppData\Local\Temp\cctQCagR.o: In function `createShip(float, float)':
main.cpp:(.text+0x4da4): undefined reference to `std::allocator<char>::allocator()'
main.cpp:(.text+0x4dbc): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons
t&)'
main.cpp:(.text+0x4de4): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x4e04): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x4e1c): undefined reference to `std::allocator<char>::~allocator()'
main.cpp:(.text+0x4e28): undefined reference to `std::allocator<char>::allocator()'
main.cpp:(.text+0x4e40): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons
t&)'
main.cpp:(.text+0x4e60): undefined reference to `std::allocator<char>::~allocator()'
main.cpp:(.text+0x4e70): undefined reference to `__cxa_end_cleanup'
main.cpp:(.text+0x4e98): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x4eb8): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x4ed0): undefined reference to `std::allocator<char>::~allocator()'
main.cpp:(.text+0x4ef4): undefined reference to `std::allocator<char>::~allocator()'
main.cpp:(.text+0x4f04): undefined reference to `__cxa_end_cleanup'
C:\Users\Bill\AppData\Local\Temp\cctQCagR.o: In function `load_files()':
main.cpp:(.text+0x5164): undefined reference to `std::allocator<char>::allocator()'
main.cpp:(.text+0x517c): undefined reference to `std::basic_string<char, std::char_tra
its<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> cons
t&)'
I believe you're trying to compile main.cpp with gcc instead of g++.
#include <string>
#include <stdio.h>
int main()
{
std::string bla;
bla = "BLA BLA";
printf("%s\n",bla.c_str());
return 0;
}
If you build the above code snippet with gcc you get the errors you mention.
If you use g++ it build ok, this makes sense since g++ will make sure all the proper stuff it put together when build C++.
You need to link your binary with libstdc++. You need to explicitly specify it in command line if using gcc:
gcc -lstdc++ tmp.cpp
If using g++, libstdc++ will be linked by default:
g++ tmp.cpp