gcc makefile hello world

hi

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?
all: hello
	-std=c++11

objects = hello.o

edit: $(objects)
	cc -o edit $(objects)

$(objects) : hello.h
hello.o : hello.h

.PHONY : clean
clean :
	-rm edit $(objects)

any help?
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.
iostream.h is deprecated. Try #include <iostream> instead.
I don't think gcc includes the standard template library by default.

That's because gcc is a C compiler. Use g++.
So what can I do, I recon, I have to link that default libary in my makefile, but I don't know how to do it
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.

all: hello
	-std=c++11

objects = hello.o

edit: $(objects)
	cc -o edit $(objects)

$(objects) : hello.h
hello.o : hello.h

.PHONY : clean
clean :
	-rm edit $(objects)

I suppose you could always try this:
all:
	g++ -Wall -Wextra -pedantic -std=c++11 -O2 -o hello hello.cpp


Otherwise...

http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
http://www.gnu.org/software/make/manual/make.html

Topic archived. No new replies allowed.