Ubuntu: Makefile unusual behaviour



Question:

I have written a make file for a simple cpp program containing cout statements.

#This a makefile for compiling the hello world cpp program.     CC=clang++     all: run test.o      run: .cpp=.o    .PHONY: clean     clean:          rm -rf *.o run  

but this is not compiling my test.cpp. From a tutorial I have taken this idea that make is intelligent enough to compile dependency and target if nothing is specified.

What is going wrong?

Thanks.


Solution:1

In your case there is no need for a makefile, since make has built-in rules that know how to compile a simple program.

Easy way

  1. Create a hello world test program named test.cpp.

    #include <iostream>  using namespace std;    int main()   {      cout << "Hello, World! << endl;      return 0;  }  
  2. Change directories with cd to the directory containing test.cpp and runmake.

    make CC=g++ test    

    In order to run the above command make and g++ must be installed.

  3. Run the test executable.

    ./test    
  4. Results of ./test :

    Hello, World!   

Hard way

  1. Create a hello world test program named test.cpp, the same as in the easy way.

  2. Change directories with cd to the directory containing test.cpp and create a makefile named makefile1.

    CC      = clang++  CFLAGS  = -g  RM      = rm -f    default: all  all: Hello  Hello: test.cpp      $(CC) $(CFLAGS) -o Hello test.cpp  clean veryclean:      $(RM) Hello    

    The two indented lines must begin with tabs, not 4 spaces. The above makefile will also run correctly if you replace clang++ with g++ .

  3. Runmake.

    make -f makefile1    

    In order to run the above command make clang and g++ must be installed.

  4. Run the Hello executable.

    ./Hello    
  5. Results of ./Hello :

    Hello, World!   

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »