Tuesday, April 3, 2012

C++ first program

A few days ago I thought it would be good to renew my knowledge about C++. I decided to create simple application and compile it on Linux and Windows. Here is application source code:
#include <iostream>

int main() {
 std::cout << "My first program..." << std::endl;
 return 0;
}

Linux

I created makefile which allows to compile and run application. To read more about how to create makefile you can read here.

CPP = g++
CFLAGS = -Wall -lrt

all: program
 $(CPP) $(CFLAGS) -o bin/program obj/program.o

program:
 $(CPP) $(CFLAGS) -o obj/program.o -c src/program.cpp

clean:
 rm -f ./obj/*.o
 rm -f ./bin/program

run:
 ./bin/program
To compile application just type make:
make
If you want to run application, type such command:
make run

To compile application on Linux I used GNU C++ compiler (g++). The -Wall command line option enables all warnings. If you want to get more information about compiler read here.

Windows

I created .mak file which allows to compile and run application. To read more about how to create .mak file and use NMAKE utility you can read here.

CPP = cl
CFLAGS = /EHsc /W4

all: program
 $(CPP) $(CFLAGS) /Febin/program.exe  obj/program.obj

program:
 $(CPP) $(CFLAGS) /c src/program.cpp /Foobj/

clean:
 del bin\*.exe obj\*.obj

run: 
 bin\program.exe
To compile application on Windows I used Microsoft C/C++ compiler (cl). The /EHsc command line option instructs the compiler to enable C++ exception handling. The /c command line option instructs the compiler to compile without linking. The /W command line option sets warning level. If you want to get more information about compiler read here.
To compile application, type such command:
nmake -f build.mak
To run application, type such command:
nmake -f build.mak run

No comments:

Post a Comment