When I compile a simple C++ program like this:
#include<iostream>
using namespace std;
int main()
{
cout << "hello word" << endl;
return 0;
}
I got some error message,
This is part of the error message:
In file included from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\cstdlib:75,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ext\string_conversions.h:41,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\basic_string.h:6391,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\string:52,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\locale_classes.h:40,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\ios_base.h:41,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:42,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from e:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from F:\Desktop\web\web\work_one\test.cpp:1:
e:\mingw\include\stdlib.h:90:1: error: '_BEGIN_C_DECLS' does not name a type
_BEGIN_C_DECLS
^~~~~~~~~~~~~~
e:\mingw\include\stdlib.h:363:1: error: '__CRT_ALIAS' does not name a type
__CRT_ALIAS __cdecl __MINGW_NOTHROW
^~~~~~~~~~~
e:\mingw\include\stdlib.h:367:1: error: '__CRT_ALIAS' does not name a type
__CRT_ALIAS __cdecl __MINGW_NOTHROW
^~~~~~~~~~~
e:\mingw\include\stdlib.h:444:55: error: '_locale_t' has not been declared
__int64 _wcstoi64_l(const wchar_t *, wchar_t **, int, _locale_t);
^~~~~~~~~
e:\mingw\include\stdlib.h:447:65: error: '_locale_t' has not been declared
unsigned __int64 _wcstoui64_l(const wchar_t *, wchar_t **, int, _locale_t);
^~~~~~~~~
e:\mingw\include\stdlib.h:866:1: error: '_END_C_DECLS' does not name a type
_END_C_DECLS
^~~~~~~~~~~~
This seems to be a problem with the header file syntax, but I did not modify the head file.
I use this command to compile:
g++ -g -std=c++11 F:\Desktop\web\web\work_one\test.cpp -o test.exe
My operating system is Win10.
The g++ version is g++ (MinGW.org GCC-8.2.0-3) 8.2.0
G++ I got from http://www.mingw.org/
Possible solution to this already exists here
Just change -std=c++11 with -std=gnu++11
Finally, I fixed the problem. The reason for this problem is that I installed codeblocks.
codeblocks adds environment variables to my computer, like C_INCLUDEDE_PATH, CPLUS_INCLUDE_PATH, and LIBRARY_PATH. When I deleted these environment variables, the problem was fixed.
Related
Im setting up the bsplib (https://github.com/Zefiros-Software/BSPLib) on a windows system (in VS Code) using WSL. When compiling I get the error message:
test.cpp:4:5: error: conflicting declaration of C function ‘int main()’
4 | int main()
| ^~~~
In file included from /mnt/d/study/software/bsp/include/bsp/bspExt.h:30,
from /mnt/d/study/software/bsp/include/bsp/bsp.h:34,
from test.cpp:2:
/mnt/d/study/software/bsp/include/bsp/bspClass.h:59:12: note: previous declaration ‘int main(int, char**)’
59 | extern int main(int argc, char **argv);
The program is used is just a bare example for BSP:
#include <iostream>
#include "bsp/bsp.h"
int main()
{
bsp_begin(bsp_nprocs());
int s = bsp_pid();
int p = bsp_nprocs();
printf("Hello World from processor %d / %d", s, p);
bsp_end();
return 0;
}
Compiled with:
g++ -I/mnt/d/study/software/bsp/include -g -lpthread -o main test.cpp
To my (quite limited) knowledge, the 'extern' in the header file should prevent the compiler from labelling the main as 'duplicate' of some sort. Im mostly interested in some of BSPs functionalities as part of a class of mine, that sadly does not include any support on the installation. What I've done so far:
Copied the include files from the repo
Added the include path to the compilation (-I Flag) and the -lpthread as instructed by the class script
Added the include path to the configuration (c_cpp_properties.json) [tested both with and without this, no difference]
Due to the many possible sources of that error (program, compiler, wsl, library, configuration, vs code, my stupidity) I cant determine where I am mistaken, nor am I able to find online resources to that combination.
I am unable to compile a basic boost vector example.
I am on Windows 10, and I am using the nuwen MinGW distro version 15.0, without git included. This version contains GCC 7.10 and Boost 1.64. I have unpacked MinGw and placed it in the root of my file system and I am following the MinGW usage instruction A to run set_distro_paths.bat. Below is the code, which is failing to build on my system:
vector-fail.cpp:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v (i) = i;
std::cout << v << std::endl;
}
Makefile:
vector-fail: vector-fail.o
g++ vector-fail.o -o vector-fail
vector-fail.o: vector-fail.cpp
g++ -c vector-fail.cpp -o vector-fail.o
Output:
g++ -c vector-fail.cpp -o vector-fail.o
In file included from C:\MinGW\include/boost/numeric/ublas/vector.hpp:21:0,
from vector-fail.cpp:1:
C:\MinGW\include/boost/numeric/ublas/storage.hpp: In member function 'void
boost::numeric::ublas::unbounded_array<T, ALLOC>::serialize(Archive&, unsigned int)':
C:\MinGW\include/boost/numeric/ublas/storage.hpp:299:33: error: 'make_array' is not a member of 'boost::serialization'
ar & serialization::make_array(data_, s);
^~~~~~~~~~
C:\MinGW\include/boost/numeric/ublas/storage.hpp:299:33: note: suggested alternative: 'make_nvp'
ar & serialization::make_array(data_, s);
^~~~~~~~~~
make_nvp
C:\MinGW\include/boost/numeric/ublas/storage.hpp: In member function 'void boost::numeric::ublas::bounded_array<T, N, ALLOC>::serialize(Archive&, unsigned int)':
C:\MinGW\include/boost/numeric/ublas/storage.hpp:494:33: error: 'make_array' is not a member of 'boost::serialization'
ar & serialization::make_array(data_, s);
^~~~~~~~~~
C:\MinGW\include/boost/numeric/ublas/storage.hpp:494:33: note: suggested alternative: 'make_nvp'
ar & serialization::make_array(data_, s);
^~~~~~~~~~
make_nvp
make: *** [Makefile:5: vector-fail.o] Error 1
Unfortunately none of those errors are occurring within my code, rather they are caused by files within include files within the boost library its self. What changes could be made in application level code or the Makefile to allow the program to compile?
Yes this is an issue with the ublas headers. I ran into it before. You can workaround it by including
#include <boost/serialization/array_wrapper.hpp>
before that point though. I'd consider reporting it to the maintainers of the ublas code.
I'm trying compile this code in cmd:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 24;
string s = to_string(n);
cout<<s;
}
I found in another qestions, that I should use this command:
g++ -std=c++11 main.cpp
but I get error:
main.cpp: In function 'int main()':
main.cpp:7:24: error: 'to_string' was not declared in this scope
string s = to_string(n);
^
I have latest g++ compiler for Windows v. 5.3.0. I used this installer for MinGW.
I tried solve in to_string is not a member of std, says g++, but this patch doesn't work.
How can I compile this code in command line in Windows?
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem
While building an existing code base on Mac OS using its native build setup I am getting some basic strange error while compilation phase.
Does any of you have any idea, as I have seen it's been discussed earlier as well in this forum without any good reason. I can not see any conflicting files being included.
But still I am unable to compile the code because this error appears.
Source are like the code given below and compilation error appears
$ cat a.h
#include <string>
#include <sstream>
namespace brijesh {
typedef std::string String;
template<class T>
String toString(T value) {
std::ostringstream buffer;
buffer << value;
return buffer.str();
}
$ cat b.h
#include "a.h"
namespace brijesh {
class Platform {
public:
static String getName();
};
}
$ cat b.cpp
#include "b.h"
namespace brijesh {
String Platform::getName()
{
String name = "UNKNOWN";
#ifdef LINUX
name = "linux";
#endif
#ifdef MACOSX
name = "Mac";
#endif
return name;
}
}
flags used for compilation
g++ -c -o test.o -DRELEASE_VERSION -ggdb -arch ppc -mmacosx-version-min=10.4 -pipe -fpermiss ive -nostdinc -nostdinc++ -isystem /Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3 .3 -I/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++ -I/Developer/SDKs/MacOS X10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin -isystem /Developer/SDKs/MacOSX10.3.9. sdk/usr/include -F/Developer/SDKs/MacOSX10.3.9.sdk/System/Library/Frameworks -Wreturn-type -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -Wall -Wno-multichar -Wno-unk nown-pragmas -Wno-long-double -fconstant-cfstrings -MP -MMD x.cpp
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/bits/locale_facets.h: In constructor 'std::collate_byname<_CharT>::collate_byname(const char*, size_t)':
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/bits/locale_facets.h:1072: error: '_M_c_locale_collate' was not declared in this scope
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin/bits/messages_members.h: In constructor 'std::messages_byname<_CharT>::messages_byname(const char*, size_t)':
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/ppc-darwin/bits/messages_members.h:79: error: '_M_c_locale_messages' was not declared in this scope
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits: At global scope:
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: 'float __builtin_huge_valf()' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: 'float __builtin_huge_valf()' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:897: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: 'float __builtin_nanf(const char*)' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: 'float __builtin_nanf(const char*)' cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:899: error: a function call cannot appear in a constant-expression
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:900: error: field initializer is not constant
/Developer/SDKs/MacOSX10.3.9.sdk/usr/include/gcc/darwin/3.3/c++/limits:915: error: field initializer is not constant
It looks like you're trying to use OS X 10.3 developer tools (Xcode et al) and are trying to target OS X 10.4, which is obviously not going to work. Either change your build command to remove incompatible flags, such as -mmacosx-version-min=10.4, or upgrade to a more current version of OS X + Xcode + SDKs.