Thread: Easy Makefile Test

  1. #1
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130

    Easy Makefile Test

    I want to have makefiles for both c and c++ in different file folder locations. The c makefile here was nice and easy (the file needs to be called Exercises.c for this makefile...I did not set it up so that I can rename it with an environment variable). My question is, how come the c++ makefile is more difficult. I could not make it very simple like the c makefile, I had to do it a lot differently. Is there an easier way to do the c++ makefile (the file for the c++ makefile needs to be called Exercises.cpp).

    The C Makefile:
    Code:
    P=Exercises
    OBJECTS=
    CFLAGS= -g -Wall -o3 -std=gnu11
    LDLIBS=
    CC=gcc
    
    $(P): $(OBJECTS)
    The C++ Makefile
    Code:
    P=Exercises
    OBJECTS= 
    CPPFLAGS= 
    CXXFLAGS= 
    LDLIBS= 
    CXX=g++
    
    $(P): $(P).o
        g++ -g -o $@ $(P).o
    
    $(P).o : $(P).cpp
        g++ -g -Wall -std=gnu++14 -c $(P).cpp
    
    clean:
        rm $(P).o $(P)

  2. #2
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    I updated the C++ makefile. It is a bit better this way: Anyway, it appeared that I had to use two lines because the -c option compiled the code but did not link, whereas with gcc the flags do both the compile and the link and there isn't even an object file because it automatically takes both steps.
    Code:
    P=Exercises
    OBJECTS= 
    CPPFLAGS= 
    CXXFLAGS= 
    LDLIBS= 
    CXX=g++
    
    $(P): $(P).o
        g++ -g -o $@ $<
    
    $(P).o : $(P).cpp
        g++ -g -Wall -std=gnu++14 -c $<
    
    clean:
        rm $(P).o $(P)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use "gnu++14". Use "c++14". Stick to the standard!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    This is more like the answer that I was looking for. I did not have the -o flag, so I could get it to compile into a .o file, but it would not link until I added that flag, and also the -c needed to be after the -o otherwise it would not work. Anyway, I should be able to set these other variables now and hopefully it will work out. It took a long time to figure out, btw Elysia, have you tried the Intel compiler? As far as the c++14 flag, I will look it up. When I looked up how to set up c++14, the search said to use gnu++14 and that did compile the variable in my code which I initialized with the new style. I would not know just how much of the new standard gnu++ supports.
    Code:
    P=Exercises
    OBJECTS= 
    CPPFLAGS= 
    CXXFLAGS= -std=gnu++14 -g -Wall -o -c
    LDFLAGS= 
    LDLIBS= 
    CXX=g++
    
    $(P): $(OBJECTS)
    
    clean:
        rm $(P)
    And
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
        int n {12};
        
        cout << "Hello" << n << endl;
        return 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I do not use Linux, so I do not use make files. I do not use gcc or clang as my main compiler, so I don't know (without looking it up) how to compile by producing object files and then linking them together.
    I have used Intel's compiler, but that was long ago. When I did use it, I used it within the Visual Studio IDE. I have not done much command line compiling with the compiler.
    gnu++14 is telling the compiler to target C++14, but enable GNU extensions. Unless you absolutely have need of those extensions, don't use them. So compile with "c++14" instead and follow the standard to get maximum portability between compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The compilation rule can be more general than that.
    Code:
    CXX=g++
    OBJ=Exercises.o
    CXXFLAGS= -Wall -std=gnu++14
    
    %.o: %.cpp
         $(CXX) -c -o $@ $< $(CXXFLAGS)
    
    Exercises: $(OBJ)
        $(CXX) -o $@ $^ ($CXXFLAGS)
    
    clean:
        rm $(OBJ)
    The above should work just fine. The rule says that the .O version depends on the .CPP version, so when the source changes you get new .O files.

    make in general follows the first rule it encounters, so the order does matter.
    Last edited by whiteflags; 09-18-2015 at 01:47 PM. Reason: my keyboard's been fighting me...

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I'd recommend switching to an IDE that builds Makefiles for you. Code::Blocks doesn't use literal Makefiles but it'll still build for you.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by MutantJohn View Post
    I'd recommend switching to an IDE that builds Makefiles for you. Code::Blocks doesn't use literal Makefiles but it'll still build for you.
    Also an excellent idea.

  9. #9
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    Yes I will still be spending a bit more time on Makefile and thanks for the info. I have Qt Creator set up however if I want an open source project than I need to use package config, auto tools, and Makefile. My next step however looks like I will be operating the debugger. I need the gnu side, but it should also be POSIX compatible.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you need GNU for? POSIX is outside the C++ standard, and compilers for that matter. That's API matter. If the API adheres to POSIX, then you're good to go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    I have to prepare to make an open source effort, although at this time I am just reading some books. What do you mean, I thought that gnu is the best. As a second language I will take python and it has lots of gnu libraries as far as I understand.

  12. #12
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    ...wasn't quite finished talking.

    I sort of just follow what the book that I am reading says to do, except that it does not tell me everything, right. I have a few different books that I am reading at this moment, one of them is a C book and that is why I wondered about Makefiles for the c++ compiler. It was not covered in my book. Gnu works okay for me and I've only heard of the Gnu Scientific Library so far. The Makefile lets me add more libraries to the compiler and linker via include paths and lib paths instead of typing them out manually every time I update my source files.

    ...and that is all that I know :+p

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The GNU flag enables a set of GNU extensions to the compiler. It's not a standard. Unless you need those extensions (and don't rely on them because they're not standard), you should not be using that flag. Unless you're using an external library that requires the flag to be set (unlikely), don't use it. You will be better off. The GNU flag has nothing to do with the GNU Scientific Library or other external libraries.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    The Gnu option is my only choice here, I believe, and I almost have things under control now. I can see the path better now that I have blundered enough times. The Gnu is the light, to brighten the way it is full of peace, goodness, and fruitful experiences.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't be silly. GNU is not your only choice. It's evil. Stop using it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy to use random number test suite?
    By Sebastiani in forum General Discussions
    Replies: 3
    Last Post: 07-12-2009, 08:17 AM
  2. difference makefile makefile.am makefile.in
    By Bargi in forum Linux Programming
    Replies: 7
    Last Post: 10-28-2007, 02:08 PM
  3. Test at http://www.artlogic.com/careers/test.html
    By zMan in forum C++ Programming
    Replies: 6
    Last Post: 07-15-2003, 06:11 AM
  4. easy test to speach engine
    By Andrewthegreen in forum Windows Programming
    Replies: 1
    Last Post: 10-19-2002, 02:19 PM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM