LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I compile? (https://www.linuxquestions.org/questions/programming-9/how-do-i-compile-415020/)

inverted.gravity 02-13-2006 02:22 PM

How do I compile?
 
Im trying to compile this simple ******

#include <iostream>
int main(void)

{

cout << "WORK FFS!";
cout << "\n";
return 0;

}


what have I done wrong? beacouse it says when i try to compile cc test.cpp

ali@pullansreturn:~/programing$ cc test.cpp
test.cpp: In function 'int main()':
test.cpp:6: error: 'cout' was not declared in this scope



and where does the runnable file get when if I ever succeding compiling? seriously i dont know whats wrong. HELP!

graemef 02-13-2006 02:35 PM

if you compile your program with the following command
Code:

g++ -o test.o test.cpp
you will get an output file (-o switch) with the name test.o which you can run with the command
Code:

./test.o
This file will be in the same directory as the source code. Using a path you can get a different location.

Your code is using cout which resides in the std namespace and so you will need to modify each cout, what follows is an example of what you will need to do:
Code:

std::cout << "Hello";

inverted.gravity 02-13-2006 02:51 PM

OK, I have seen how people use "using namespace std;" but it doesnt work, i guess it does the same thing as std::cout << "Hello"; But it still doesnt work


include <iostream>
int main(void)
using namespace std;

{

cout << "WORK FFS!";
cout << "\n";
return 0;

}


ali@pullansreturn:~/programing$ g++ -o test.o test.cpp
test.cpp:3: error: expected initializer before 'using'
test.cpp:5: error: expected unqualified-id before '{' token
ali@pullansreturn:~/programing$

inverted.gravity 02-13-2006 02:53 PM

#include <iostream>
using namespace std;
int main(void)
{

cout << "WORK FFS!";
cout << "\n";
return 0;

}
~
~


it works now! thanks!

spooon 02-13-2006 02:55 PM

(nevermind)

inverted.gravity 02-13-2006 03:04 PM

Yeah i know what you were going to mention, that int main(void) is the head of the { } body and that i typed using namspace std; between them

graemef 02-13-2006 03:13 PM

Indeed using std:: each time you use a function from the standard libraries is the same as putting in a using namespace std; at the start of the file however I have a preference towards std:: because it makes it quite clear that it is a standard function that is being used.

Also you can replace a "\n" with a std::endl which can all be on one line as follows:
Code:

std::cout << "Hello" << std::endl;
graeme.

inverted.gravity 02-13-2006 03:19 PM

well to make this clear, im a total noob in all aspects of programing, i just started for like an half hour ago :)
so i dont really know what to do, i dont even know what using namespaces std; is not even int main(void) lol :)


All times are GMT -5. The time now is 03:28 AM.