I've got a simple hello world example that I'm trying to compile on OS X, named hw.cpp:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'd like to compile it using gcc, but I've had no success. I'd also like to hear the other options, like using Xcode ?
Try
g++ hw.cpp
./a.out
g++ is the C++ compiler frontend to GCC.
gcc is the C compiler frontend to GCC.
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
Though I prefer a slightly more verbose approach:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
g++ hw.cpp -o hw
./hw
user#host> g++ hw.cpp
user#host> ./a.out
Compiling it with gcc requires you to pass a number of command line options. Compile it with g++ instead.
Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).
The new version of this should read like so:
xcrun g++ hw.cpp
./a.out
Use the following for multiple .cpp files
g++ *.cpp
./a.out
You didn't specify what the error you're seeing is.
Is the problem that gcc is giving you an error, or that you can't run gcc at all?
If it's the latter, the most likely explanation is that you didn't check "UNIX Development Support" when you installed the development tools, so the command-line executables aren't installed in your path. Re-install the development tools, and make sure to click "customize" and check that box.
Related
Total novice C++ user going thru tutorials using Visual Studio Code on OSX. Barest bones Hello World program
#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}
Then I run-compile in VSCodes terminal using
$ g++ Foo.cpp -o foo
My question is, shouldn't I be seeing the Hello, World! out put in the terminal? Or will this only be visible if I compile and run in Windows?
I do see an executable after compiling but nothing in the VS Terminal window.
You actually have to run the file. So, in this case, since you named your file "foo" with the -o command, you have to run ./foo from the terminal.
I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.
The last days I began learning C++. As with every language I began making a Hello World program. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "I'm doomed in Windows :(" << endl;
}
I compiled it with g++ (from MinGW) like this:
g++ -c hello.cpp
g++ -o hello hello.o
The output is:
hello.exe
But when I open hello.exe it freezes / doesn't start. I tried both to open it via the command line as opening it with the GUI. It looks like this:
https://drive.google.com/file/d/0ByNRkSnhavxIaXh6a2JSaEpZV0k/view?usp=sharing
Does anybody know what's wrong? Thanks in advance!
PS: If you wonder why I didnt post the image inline, I can't. I need 10 reputation points for that ^^.
The problem is solved by deactivating Avast, thanks to Hans Passant. It is also possible to add the executable to the Avast exclusion list.
I am trying to compile some super simple code in Code::Blocks using GCC 4.4. I am on OS X Mavericks if that matters. I'm trying to compile and make a 32 bit executable, so I've taken the advice of a bunch of stack overflow posts to add the -m32 flag to the compiler (in Code::Blocks, I did Project > Build Options > Compiler Settings > Other Options > added -m32).
The code is the standard hello world program that pops up in every main file. I'll post it here for those who aren't familiar with Code::Blocks.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
When I go to build, I clean all the object files and rebuild, and I get the following warning which prevents an executable from being created:
||warning: ignoring file obj/Release/main.o, file was built for i386 which is not the architecture being linked (x86_64): obj/Release/main.o|
I'm not really sure where to go from here. I thought the -m32 flag deals with these things.
Any ideas or directions I can take? If there's any info I left out let me know.
I just installed Ubuntu and tried making the famed "Hello World" program to make sure that all the basics were working. For some reason though, g++ fails to compile my program with the error: "'cout' is not a member of 'std'". I've installed the build-essential package. Am I missing something else?
#include <iostream.h>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Looks pretty good to me...
Use #include <iostream> - iostream.h is not standard and may differ from the standard behaviour.
See e.g. the C++ FAQ lite entry on the matter.
The standard header is called <iostream>, not <iostream.h>. Also, it is agood idea to compile your C++code with the -Wall and -pedantic flags, which can point out lots of errors with non-standard code that g++ would otherwise ignore. Use:
g++ -Wall -pedantic myprog.cpp
Sounds like it did find iostream.h but it does not define cout in the std namespace. It is there for backwards compatibility with older programs that expect cout to be in the global namespace.
use
#include<iostream>
using namespace std;
without namespace you won't be able to use cout or cin