0

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.

0

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! 
    
  • but what is wrong with my makefile. I want these types of small make files for the testing of small small programs for PoC before going for actual research development projects. – Amarjeet Sharma Aug 6 '17 at 7:19
  • I rewrote your makefile program so that it works, and added the code of the makefile to my answer. In my makefile there are three variables named CC, CFLAGS and RM. – karel Aug 6 '17 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.