this code:
#include <iostream>
#include <string>
int main()
{
std::string s = "52123210";
int derp = std::stoi(s, 0, 10);
std::to_string(derp);
return 0;
}
with this error:
test.cpp:10:2: error: 'stoi' is not a member of 'std'
test.cpp:11:2: error: 'to_string' is not a member of 'std'
tried this:
http://tehsausage.com/mingw-to-string
(not work)
Update my MingW from 4.6.1 to 4.8.1
(not work)
possible bug:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522
(beyond of my knowledge to apply anything, I don't dare to touch the compiler's code)
**Also not work with "using namespace std" but produced 'stoi' and 'to_string' not declared
error instead.
This is a result of a non-standard declaration of vswprintf on Windows. The GNU Standard Library defines _GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use.
https://stackoverflow.com/a/8543540/2684539 proposes a hack/work around.
Related
Im writing a shared library for a FreeBSD application.
This library gets loaded by LD_PRELOAD.
This application has multiple compile-versions, so some function offsets might change and my library wont work there.
Now i want to read the offsets at loading the library.
The offsets are changing, so i think my only way is to read the offsets of specific function names.
The offsets are simply the offsets of functions or labels.
Now the problem - how to do it?
Example
In the first version, i call the main version like that:
int(*main)(int argc, char *argv[])=(int(*)(int,char*[]))0x081F3XXX;
but in the second, the offset has changed:
int(*main)(int argc, char *argv[])=(int(*)(int,char*[]))0x08233XXX;
Programmers (me) are lazy and don't want to compile their libs for every version.. I want to create a lib, that is for every version!
I simply need the offsets of the functions via function name, the rest is no problem..
Thats how i call the library:
LD_PRELOAD="/path/to/library.so" ./executable
or
env LD_PRELOAD="/path/to/library.so" ./executable
Edit with test code
Here my testcode regarding to the comments:
Main.cpp:
#include <stdio.h>
void test() {
printf("Test done.\n");
}
int main(int argc, char * argv[]) {
printf("Program started\n");
test();
}
lib.cpp
#include <stdio.h>
#include <dlfcn.h>
void __attribute__ ((constructor)) my_load(void);
void my_load(void) {
printf("Library loaded\n");
printf("test - offset: 0x%x\n",dlsym(NULL,"test"));
}
test.sh
g++ main.cpp -o program
g++ -shared lib.cpp -o lib.so
env LD_PRELOAD="lib.so" ./program
-> Result:
Library loaded
test - offset: 0x0
Program started
Test done.
Does not seem as would it work :s
Edit 15:45
printf("test - offset: 0x%x\n",dlsym(dlopen("/home/test/test_proc/program",RTLD_GLOBAL),"test"));
This also does not work.. Maybe dlsym is the wrong way?
I reproduced your program on Mac OS X using Clang, and found a solution. First, the boring parts:
To make it compile cleanly I had to change your %x format specifier to %p for the pointer.
Then, on Mac OS X I had to pass RTLD_MAIN_ONLY as the first argument to dlsym(). I guess this is platform-dependent; on Linux it does seem to be NULL as you have.
Now, the meat of the fix!
You're searching with dlsym() for a symbol called test. But there is no such symbol in your application. Why? Because you're using C++, and C++ does "name mangling." You could use any number of tools to figure out the mangled name and try to load that with dlsym(), but it could change with different compilers. So instead, just inhibit name mangling by enclosing your test() function in extern "C":
extern "C" {
void test() {
printf("Test done.\n");
}
}
This fixed it for me:
$ DYLD_INSERT_LIBRARIES=lib.so ./program
Library loaded
test - offset: 0x1027d1eb0
Program started
Test done.
I'm trying to use to_string to append an integer value to a string like this:
#include <iostream>
#include <string>
using namespace std;
int main(){
int number = 1;
string p = "Player";
p += to_string(number);
return 0;
}
But when I try to compile it I get an error stating to_string was not declared in this scope, so I removed the using namespace std; and replaced to_string with std::to_string, but now I get a new error saying that to_string is not a member of std.
This problem only occurs when I compile from the command line but it works perfectly in XCode.
Any ideas on why this is? (I need to be able to compile it from the command line for a hw assignment)
to_string is a feature of C++11, did you use the flag --std=c++11?
In my program I try to convert a int to a char[20];
I try to do this in the following way:
char str[20];
sprintf(str, "%d", timer);
in which timer is the int.
But when I build this code, I get the following warnings.
Type implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]
incompatible implicit declaration of built-in function 'sprintf' [enabled by default]
what does that mean?
note:( I have included string.h and stdlib.h).
great, I added stdio.h to my code and now the warnings disappeared only to give me a even harder error.
undefined reference to `_sbrk'
You have to #include <stdio.h> to use sprintf()
you want to make sure you also add reference to stdio.h see this ref
You probably need to put sprintf(str, "%d", timer) inside a function (not on the global part of the source code).
Something like:
#include <stdlib.h>
char str[20];
// SPOT #1
int f() {
sprintf(str, "%d", timer); // this won't work if placed on SPOT #1
}
On compiling code at Warning Level 4 (/W4), I get C4996 warnings on std::copy() calls whose parameters are C arrays (not STL containers like vectors). The recommended solution to fix this seems to be to use stdext::checked_array_iterator.
What is the use of stdext::checked_array_iterator? How does it work?
Why does it not give any compile warning on this piece of erroneous code compiled under Visual C++ 2010?:
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
int arr0[5] = {100, 99, 98, 97, 96};
int arr1[3];
copy( arr0, arr0 + 5, stdext::checked_array_iterator<int*>( arr1, 3 ) );
return 0;
}
This page, Checked Iterators, describe how it works, but this quote sums it up: Checked iterators ensure that you do not overwrite the bounds of your container.
So if you go outside the bounds of the iterator it'll either throw and exception or call invalid_parameter.
I'm trying to compile VC6 project with VC10...
I obtain an error C2678 with set_intersection: I wrote some example to understand. Can anybody explain how to compile this snippets ?
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
int main( )
{
using namespace std;
typedef set<string> MyType;
MyType in1, in2, out;
MyType::iterator out_iter(out.begin());
set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), out_iter);
}
The output :
c:\program files\microsoft visual\studio 10.0\vc\include\algorithm(4494): error C2678: '=' binary : no operator defined which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
If I use a std::vector instead of std::set the compilation succeeded.
acceptable)
Try
set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), inserter(out, out.begin()) );
This is because set_intersection wants to write to the output iterator, which causes the output container to grow in size. However this couldn't be done with just an iterator alone (it could be used to overwrite existing elements but not grow in size)
Edit: fixed the typo. Use inserter for adding to a set. A back_inserter only works for vectors and such.
Edit 2: fixed another typo. STL inserter requires a second argument which is a hint iterator to the likely insert position. Thanks chepseskaf.