On one of my Mac box, for simple Hello Word C++ program, there are such compile error, other Macs I am working on are ok. Using even the same version of Eclipse CDT 64-bit Mars.
Posted error and Hello Word program, does anyone have any hints? Thanks.
//============================================================================
// Name : Test1.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Attach error from g++ of command line,
g++ Test1.cpp
Test1.cpp:9:20: error: iostream: No such file or directory
Test1.cpp: In function ‘int main()’:
Test1.cpp:13: error: ‘cout’ was not declared in this scope
Test1.cpp:13: error: ‘endl’ was not declared in this scope
In summary, if whether you're developing in C, C++ or Objective C on the Mac, you probably just want to use XCode.
Not sure what Eclipse was doing, but (see link in comments) earlier versions of Eclipse might work where later ones will not.
Related
I am trying to compile the simple Hello World! program from this tutorial LEDA Tutorial, section 1.3 using LEDA 6.1 on the UNIX system of my department.
#include <LEDA/core/string.h>
#include <iostream>
using leda::string;
using std::cout;
int main ()
{
string msg = "Hello World!";
cout << msg << "\n";
}
After setting the environment variable
LEDAROOT = /usr/local/LEDA-6.1
export LEDAROOT
I try to compile the file leda1.c containing the upper code
g++ -c leda1.c -I$LEDAROOT/incl
I get some errors witch are:
In file included from /usr/local/LEDA-6.1/incl/LEDA/system/basic.h:70,
from /usr/local/LEDA-6.1/incl/LEDA/core/string.h:16,
from leda1.c:1:
/usr/local/LEDA-6.1/incl/LEDA/system/misc.h: In function ‘int leda::Max_Value(int&)’:
/usr/local/LEDA-6.1/incl/LEDA/system/misc.h:120: error: ‘INT_MAX’ was not declared in this scope
/usr/local/LEDA-6.1/incl/LEDA/system/misc.h: In function ‘int leda::Min_Value(int&)’:
/usr/local/LEDA-6.1/incl/LEDA/system/misc.h:121: error: ‘INT_MAX’ was not declared in this scope
Can anyone help me with this problem?
I've got some code that I'm running on Mac OS X that can't be compiled on the Virtual Machine running Linux Mint. This is a simple example. When I run it in Mac, all is fine, but I'm getting issues when I run the same code on Linux, so I'm assuming the library I'm including is not there, but should I be getting an include error then?
Here's the example code that runs on Mac.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
for (int i = 0; i < 10; i++){
string test = to_string(i);
cout << test << endl;
}
cout << "done" << endl;
return 0;
}
I get no issues here but running on Linux Mint, I get this when I try to compile:
for.cpp: In function 'int main()':
for.cpp:7:28 error: 'to_string' was not declared in this scope
string test = to_string(i);
^
make: *** [for] Error 1
Am I missing something? Any help would be much appreciated!
edit
I realize I forgot to include <string> on here and I fixed it, but what I changed (<string> included) still doesn't compile on Linux. I've used to_string before. I know that much in C++. I also tried adding <cstdlib>. Once again, this DOES compile on Mac and DOES NOT compile on Linux.
Here is my OSX output:
0
1
2
3
4
5
6
7
8
9
done
Here is my output on Linux Mint (Once again, Virtual Box, g++ make):
test.cpp: In function ‘int main()’:
test.cpp:9:28: error: ‘to_string’ was not declared in this scope
string test = to_string(i);
^
make: *** [test] Error 1
You could reproduce the problem yourself if you don't believe me. It's the same code, you can see for yourself if you want.
Compile your for.cpp file like this:
g++ -std=c++11 for.cpp
and run it with:
./a.out
The support for the to_string function in the <string> header was added in the C++11 version of the language, so you need to tell GCC to use that version. You can use the c++0x flag too, for example:
g++ -std=c++0x for.cpp
And you don't have to worry about <cstdlib>, that has nothing to do with it...
to_string() is defined in <string> if you are compiling with C++11 (but is not defined, or unreliably defined as an extension feature, if you are compiling with an earlier version of C++).
Reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string
SOLUTION:
I found a better solution. For some reason, I've read stdlib.h will not work on some linux systems. I used a different function to convert int to string.
on linux:
#include <stdio.h>
and then
for (int i = 0; i < 10; i++){
char buffer[10];
sprintf(buffer,"%d",i);
string stringInt = buffer;
cout << stringInt << endl;
// do whatever you want with the string
}
edit
To the person that down voted my solution to this, here's a post from six years ago basically saying the same thing.
I'm having some trouble using Notepad++ to compile code. I've installed notepad++ (and NppExec), downloaded MinGW from this source (http://nuwen.net/mingw.html) and installed it to "C:\MinGW\".
Then I tried to set notepad++ to use g++ to compile c++. Per advice, I entered the following into NppExec's console:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++.exe -g "$(FILE_NAME)"
Saved it as C++ Compiler, and added it to the "Macros" section of the toolbar.
Then I tried to run a simple test program:
#include <iostream>
int main()
{
cout << "Hello, world!";
}
After that a couple of weird errors popped up. First it wanted me to save to System32 by default, which I don't remember it doing before (it won't let me, forcing me to save in Documents).
I let it save to documents, than tried to run it with the compiler. It gives me this error, which I don't recognize at all:
NPP_EXEC: "C++ Compiler"
NPP_SAVE: C:\Users\Bova\Documents\Test.cpp
CD: C:\Users\Bova\Documents
Current directory: C:\Users\Bova\Documents
C:\MinGW\bin\g++.exe -g "Test.cpp"
Process started >>>
Test.cpp: In function 'int main()':
Test.cpp:5:5: error: 'cout' was not declared in this scope
cout << "Hello, world!";
^
Test.cpp:5:5: note: suggested alternative:
In file included from Test.cpp:1:0:
c:\mingw\include\c++\4.8.2\iostream:61:18: note: 'std::cout'
extern ostream cout; /// Linked to standard output
^
<<< Process finished. (Exit code 1)
Please help.
There is nothing wrong with your compiler. You are not using the correct namespace to use cout
#include <iostream>
int main()
{
std::cout << "Hello, world!";
}
Or
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
}
Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags
When compiling the following code:
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout << "Hello World!" << endl;
}
int main()
{
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
return 0;
}
using:
$ g++-4.6.1 -std=c++0x -pthread threading.cpp
I get the following error:
threading.cc: In function ‘int main()’:
threading.cc:13:2: error: ‘thread’ was not declared in this scope
threading.cc:13:9: error: expected ‘;’ before ‘t’
threading.cc:14:2: error: ‘t’ was not declared in this scope
This is on MacOSX Lion with a custom built gcc 4.6.1. All the other c++0x features that are valid for gcc 4.6 works like a charm. Is this a MacOSX specific error?
std::thread (and the rest of the C++11 thread library) is only available for some of the platforms supported by gcc 4.6.1. Unfortunately for you, MacOSX is not one of those platforms.
My commercial Just::Thread library provides the C++11 thread facilities for 32-bit MacOSX with gcc 4.5, but gcc 4.6 is not supported yet.
See http://gcc.gnu.org/PR50196 - Mac OS X doesn't support some parts of pthreads that we rely on. Building the latest version won't help, but it might be fixed for GCC 4.7