I am seeing these GDB warnings when I complied the below code with intel compiler 14.
I am using eclipse Kepler in RHEL6.5.
Code: (example from cplusplus.com)
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo = std::make_shared<int> (10);
// same as:
std::shared_ptr<int> foo2 (new int(10));
auto bar = std::make_shared<int> (20);
auto baz = std::make_shared<std::pair<int,int>> (30,40);
std::cout << "*foo: " << *foo << '\n';
std::cout << "*bar: " << *bar << '\n';
std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n';
return 0;
}
GDB Warning:
warning: RTTI symbol not found for class 'std::num_get<wchar_t, std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> > >'
warning: RTTI symbol not found for class 'std::num_get<wchar_t, std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> > >'
warning: RTTI symbol not found for class 'std::num_get<wchar_t, std::istreambuf_iterator<wchar_t, std::char_traits<wchar_t> > >'
Build:
make -k all
Building file: ../src/test2.cpp
Invoking: Intel Intel(R) 64 C++ Compiler
icpc -g -std=c++11 -MMD -MP -MF"src/test2.d" -MT"src/test2.d" -c -o "src/test2.o" "../src/test2.cpp"
I don't see these warnings with g++ 4.72. Any issue with this warning? Thanks.
Related
The book said I cannot change the value of const once I gave it a number, but it seems I can still give it a number even if it was given.
#include<iostream>
using namespace std;
const int fansc(100);
cout<< fansc << endl; //output:100
int fansc(20);
cout<< fansc << endl;//output:20
The C++ code you gave won't compile, and rightly so. A const variable(a) is, well, ... constant. The error is shown in the following program and transcript:
#include <iostream>
using namespace std;
int main() {
const int fansc(100);
cout << fansc << endl;
int fansc(20);
cout << fansc << endl;
}
pax> g++ --std=c++17 -Wall -Wextra -Wpedantic -o prog prog.cpp
prog.cpp: In function ‘int main()’:
prog.cpp:6:9: error: conflicting declaration ‘int fansc’
6 | int fansc(20);
| ^~~~~
prog.cpp:4:15: note: previous declaration as ‘const int fansc’
4 | const int fansc(100);
| ^~~~~
That leaves the Anaconda bit that you mention in a comment. I have little experience with that but it seems to me the only way that would work is if the second fansc definition was somehow created in a different scope to the first. In real C++ code, that would go something like:
#include <iostream>
using namespace std;
int main() {
const int fansc(100);
cout << fansc << endl;
{ // new scope here
int fansc(20);
cout << fansc << endl;
} // and ends here
cout << fansc << endl;
}
And the output of that is:
pax> g++ --std=c++17 -Wall -Wextra -Wpedantic -o prog prog.cpp && ./prog
100
20
100
(a) Yes, I know that's a self-contradiction :-)
Function foo takes a vector of strings. It's defined as
bool foo(vector<string>& input);
When I call foo with:
foo(vector<string>{"abc"});
my compiler gives the following error:
error: expected '(' for function-style cast or type construction
and points to { as the start of the error. This compiles fine in Xcode but I get the error when running the following via command line with:
g++ -o -std=c++17 main.cpp
What is wrong with my g++ syntax?
G++ Version Information:
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
Your command line specifies that the output file ("-o") should be called "-std=c++17" – it does not say anything about the language version, so you're compiling as C++03.
Remove the "-o" or add an actual file name.
Also, note that your "g++" is an alias for clang.
I took your code and tried to compile it. For me there was rather problem with trying to pass non const value to function. I changed function argument to const and it compiled and printed without any problem.
#include <iostream>
#include <vector>
bool foo(const std::vector<std::string>& v) {
for (auto& a : v) { std::cout << a << std::endl; }
return true;
}
int main()
{
bool result = foo(std::vector<std::string> {"1", "2", "3" });
// do something with result
return 0;
}
Compiled on: https://www.onlinegdb.com/online_c++_compiler
Function foo expects for an l-value.
You are generating an instance and passing it to the function. But lifetime of the object is not enough for the pass-by-reference call.
Here is an example below; instance of class A is immediately destructed.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class A {
public:
A(int m): m(m) {
cerr << __func__ << endl;
}
~A() {
cerr << __func__ << endl;
}
int m;
};
int main() {
cerr << __func__ << endl;
A(5);
cerr << __func__ << endl;
return 0;
}
Outputs:
main
A
~A
main
This question already has answers here:
Why full specialization of template function is not picked up from the .cpp file without declaration?
(3 answers)
Closed 4 years ago.
Here's my program:
print.hpp:
#pragma once
#include <iostream>
template<size_t p>
void print()
{
std::cout << "" << __FILE__ << "" << __LINE__ << "" << std::endl;
exit(0);
}
print.cpp:
#include "print.hpp"
template<>
void print<13>()
{
std::cout << "Unlucky." << std::endl;
}
main.cpp:
#include <iostream>
#include "print.hpp"
int main()
{
std::cout << "Started." << std::endl;
print<13>();
std::cout << "Exiting." << std::endl;
}
When I compile that with g++ main.cpp print.cpp -O0 -std=c++11 && ./a.out it works fine (output is:
Started.
Unlucky.
Exiting.
).
However, if'd I compile that with g++ main.cpp print.cpp -O1 -std=c++11 && ./a.out it would give me a segmentation fault with the output:
Started.
Unlucky.
Speicherzugriffsfehler //German for memory access error
Almost the same with clang++, without optimization it would do its job just fine
and with -O1 or higher it outputs that:
Started.
Unlucky.
./print.hpp8
Why is that?
You need to declare the template specialization in the .hpp file.
template<size_t p>
void print()
{
std::cout << "" << __FILE__ << "" << __LINE__ << "" << std::endl;
exit(0);
}
// Declare the specialization.
template<> void print<13>();
Without the declaration in the .hpp file, I get a linker error with g++ 6.4.0.
.../Local/Temp/cctCC5MK.o:print.cc:(.text+0x0): multiple definition of `void print<13ul>()'
.../Local/Temp/ccgodRUG.o:socc.cc:(.text$_Z5printILm13EEvv[_Z5printILm13EEvv]+0x0): first defined here
collect2: error: ld returned 1 exit status
I am not sure how you are able to successfully build your program without the declaration.
I'm in a C++ class, and the teacher requires that we use Eclipse (I'm running it on a Macbook Pro). I've followed all of his instructions (including adding -std=c++11 to my c++ settings in eclipse) and compiled and ran a hello world program.
Then he wants us to test things from the std library. He gave us a file to use and said we should be able to compile and run it without any issues. This is the entire code:
//============================================================================
// Name : stltest.cpp
// Version : Winter 2014
// Description : Simple test of STL (really of MingGW in Eclipse)
//============================================================================
#include <map>
#include <string>
#include <iostream>
int main() {
std::map<const int,std::string> m;
m = {{1,"one"},{2,"two"},{3,"three"}};
m[4] = "four";
m[5] = "five";
for (std::pair<int,std::string> elem : m)
std::cout << elem.first << ' ' << elem.second << std::endl;
std::cout << std::endl;
for (auto elem : m)
std::cout << elem.first << ' ' << elem.second << std::endl;
std::cout << std::endl;
for (auto elem = m.begin(); elem != m.end(); elem++)
std::cout << elem->first << ' ' << elem->second << std::endl;
std::cout << std::endl;
return 0;
}
The problem is I'm getting compiler errors, and after 2 hours of searching, I can't find anything that will fix them. I think it has to do with C++ 11 and eclipse, but I'm not 100% sure. Here is what it is saying:
23:47:59 **** Build of configuration Debug for project Test0 ****
make all
Building file: ../src/stltest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/stltest.d" -MT"src/stltest.d" -o "src/stltest.o" "../src/stltest.cpp"
In file included from ../src/stltest.cpp:8:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:423:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:16:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:598:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:336:15: error: read-only variable is not assignable
first = _VSTD::forward<first_type>(__p.first);
~~~~~ ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:614:15: note: in instantiation of member function 'std::__1::pair<const int, std::__1::basic_string<char> >::operator=' requested here
{__nc = std::move(__v.__nc); return *this;}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1223:35: note: in instantiation of member function 'std::__1::__value_type<const int, std::__1::basic_string<char> >::operator=' requested here
__cache->__value_ = *__first;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:941:21: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<const int, std::__1::basic_string<char> >, std::__1::__map_value_compare<const int, std::__1::__value_type<const int, std::__1::basic_string<char> >, std::__1::less<const int>, true>, std::__1::allocator<std::__1::__value_type<const int, std::__1::basic_string<char> > > >::__assign_unique<const std::__1::pair<const int, std::__1::basic_string<char> > *>' requested here
__tree_.__assign_unique(__il.begin(), __il.end());
^
../src/stltest.cpp:16:4: note: in instantiation of member function 'std::__1::map<const int, std::__1::basic_string<char>, std::__1::less<const int>, std::__1::allocator<std::__1::pair<const int, std::__1::basic_string<char> > > >::operator=' requested here
m = { {1,"one"},{2,"two"},{3,"three"}};
^
1 error generated.
make: *** [src/stltest.o] Error 1
23:48:00 Build Finished (took 534ms)
What am I doing wrong?
I have an application that loads a library with dlopen, it looks like this:
#include <iostream>
#include <dlfcn.h>
void foo()
{
std::cout << "foo";
}
int main()
{
void* libbar = dlopen("./libbar.so", RTLD_LAZY);
if (!libbar)
{
std::cerr << dlerror() << std::endl;
return 1;
}
void(*bar)() = (void(*)())dlsym(libbar, "bar");
if (!bar)
{
std::cerr << dlerror() << std::endl;
return 1;
}
bar();
dlclose(libbar);
}
And here is libbar:
#include <iostream>
void foo();
extern "C"
void bar()
{
foo();
std::cout << "bar" << std::endl;
}
Output:
./libbar.so: undefined symbol: _Z3foov
Expected output:
foobar
How to make foo visible to libbar?
I'm actually using C++ and the real problem is with undefined symbols from constructors/member functions, but this should be very similar. I'm working on Linux with GCC 4.7.
You should compile and link main.cc with
g++ -rdynamic -Wall main.cc -o prog -ldl
The -rdynamic flag is important at link time.
And you'll better declare extern "C" those functions which you want to pass to dlsym.
See also the Program Library HowTo.