error: ‘memset’ was not declared in this scope - c++

I am trying to compile the complete example 8 provided at the end of this page: http://www.physics.wisc.edu/~craigm/idl/cmpfit.html
but I am getting this error: error: ‘memset’ was not declared in this scope
I have been looking how to solve this error and I saw that some people solved it by adding #include <string.h> to the head of the code. I tried it but I am still getting the same error.
I am using gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) on Ubuntu 12.04 LTS
I am trying to compile with:
g++ -o example example.cpp -lmpfit -lm

If you use C you should include string.h
Otherwise, if you use C++ you should use cstring
C: #include <string.h>
C++: #include <cstring>

I updated gcc to gcc-4.9 and I could compile just adding "#include " to the head of the code. Then I tried with gcc-4.8 and gcc-4.7, and again I was able to compile the code with no problem. Perhaps my previous compiler (gcc-4.6) was not correctly installed?¿. I will keep working with the last version of gcc. Thank you all for your help.

Related

Problems with compiling c++20

I'm trying to play around with some new c++20 features but I am not able to compile even some simple lines of code..
I am working on macOS and first tried to compile it in Xcode with c++2a language dialect which didn't work out and then tried to compile the code from the command line with g++ but I got similar errors.
Everything is up to date (Clang Vers 12, g++/gcc vers 10) and I know that there are still some issues with some features.
So here is an example:
import <iostream>;
#include <vector>
int main(){
std::vector<int> vec{1,2,3};
for (int e : vec) std::cout << e << std::endl;
}
compiled with:
g++ -std=c++2a -fmodules-ts test.cpp
Throws the following error:
header file <iostream> (aka '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header
So I assumed there is a problem with the header path from the toolchain which hasn't the latest updates so I tried changing the path to g++ main includes.
So I tried:
g++ -std=c++2a -fmodules-ts -I/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/ test.cpp
Which throwed this error:
test.cpp:1:8: error: header file <iostream> (aka '/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/iostream') cannot be imported because it is not known to be a header unit
import <iostream>;
^
In file included from test.cpp:2:
In file included from /usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/vector:60:
/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/bits/stl_algobase.h:59:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
2 errors generated.
Can someone help with this?
I am aware that I can make this compile using #include - this was just as an example for working with new features - I had the same issue with e.g. import .
I am really struggling with getting anything sorted - which compiler is the best usecase for c++20 and how and where can I include/locate the standardlib which has c++20 support?
And how can I use everything together in Xcode?
gcc 10 does not support this yet. You can use gcc trunk or the gcc modules branch, and you need to add -fmodules-ts to the command line.

error: 'unordered_set' is not a member of 'std'

In C++, I am trying to declare an unordered_set simply like this:
std::unordered_set<int> k;
But it is showing this error:
error: 'unordered_set' is not a member of 'std'
I am using g++ (GCC) 5.3.0 on windows using MinGW. Here are the things that I have already considered:
Adding the header file by #include <unordered_set>
Upgrading MinGW
Using the flag -std=gnu++11. (This is not generating any executable or error, not sure if it doing anything or not)
How to fix it and compile my code successfully?
Use -std=c++11 switch and specify output file.
g++ -std=c++11 your_file.cpp -o your_program

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

MinGW g++ cannot find std::stof

I just (re)installed MinGW from scratch, with gcc 4.8.1 (the latest available), and the following program won't compile:
#include <iostream>
#include <string>
int main()
{
float f;
std::string s = "5.235";
f = std::stof(s);
std::cout << f << '\n';
}
Here's the command I use:
g++ -std=c++11 -o test test.cpp
I get this error:
test.cpp:8:9: error: 'stof' is not a member of 'std'
f = std::stof(s);
^
The file bits\basic_string.h which declares std::stof is included properly in the string header, and I checked bits\basic_string.h for std::stof's declaration, and it's there.
After a bit of Googling I did find some old patches for MinGW (4.6 - 4.7) but they seem irrelevant since I'm on gcc 4.8.
Any ideas? Thanks in advance.
Not sure about exact problem, but check mingw-w64 they have gcc 4.9.2 for now. It compiles your code just well. (But since the mingw-w64 project on sourceforge.net is moving to mingw-w64.org it's better to use mingw-w64.org)
Despite of it's name it provides compilers for both x86 and x64 targets.
Probably this should be a comment, not an answer.

How can I build glib at c++ - compile error when build .cpp

All
When I build cpp using g++ and glib, I meet compile error.
c and gcc version is OK.
I'm building at Ubuntu 10.XX.
I even built it at windows - c++.
How can I build glib at c++?
Thanks you.
#include <glib.h>
int main() {
g_printf("test");
return 0;
}
jongwon_kwak#kwak-com:~/worksapce_CXX/TESTGLib/src$ g++ -c -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include TESTGLib.cpp
TESTGLib.cpp: In function ‘int main()’:
TESTGLib.cpp:12:17: error: ‘g_printf’ was not declared in this scope
you are missing #include <glib/gprintf.h>
Just edit this and it will work
#include <glib/gprintf.h>