I'm trying to code hello world in GCC, but whilst I get my simple makefile correctly, the problem comes when I try to use <iostream> or even my own headerfiles defined outside the folder with my main .cpp file.
so any help?
How should I alter the makefile below to accommodate <iostream> or any header files out side my main.cpp file folder?
For better organization, source code can be split into code files, and header files.
In good old C this means files with .c extension for code, and files with .h extension for headers.
Since C++ is basically an improved C, the same rules can be applied, and you have .cpp and .h (or .hpp) files respectively.
So what can you store in either types of files?
- You store global variable definitions and function definitions in code files.
- And you store global variable declarations, macros, templates and type definitions and function declarations in header files.
What this all means, is that you do not compile header files. You only compile code files. So there's no need to "accommodate" them in your Makefile.
but how can I include <iostream.h> in my hello.cpp file and not get linker errors. I don't think gcc includes the standard template library by default.
First of all, edit your Makefile to use g++ (C++ compiler) instead of gcc (C compiler) for your C++ source code.
Then it should just work, with linking being done automatically.
@Catfish, how do I do that? explicitly in my simple make file stated above? I seen that makefiles are quite delicate so I asking so as not to make mistakes which can be costly.