Thread: Anyone know how to write a makefile which works with any compiler?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Anyone know how to write a makefile which works with any compiler?

    I'm looking at different makefile tutorials, and it seems all of them are written to work with specific compilers. I want to know if there is way to write a makefile which works with any compiler, and across all major OSes?
    Last edited by Programmer_P; 06-08-2010 at 09:23 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Use cmake.
    CMake - Cross Platform Make
    Or jam.
    Jam -- Perforce Software

    Unfortunately makefiles aren't really all that portable since they require make (and therefore a GNU system, which is a big download for all those Visual Studio users)

    EDIT: And if you're bent on using makefiles there's always autoconf - that's the guy that makes ./configure scripts.
    Consider this post signed

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    CMake works pretty well across platforms, Bakefile *can* as long as you are using a supported platform. Regular make, VS projects et all are just good on the platforms they come on. For example (CMake):
    Code:
    cmake_minimum_required(VERSION 2.6)
    PROJECT(timer)
    ADD_DEFINITIONS( -Wall -ggdb )
    ADD_LIBRARY( timer SHARED timer.cpp )
    ADD_EXECUTABLE(timer_test timer_main.cpp)
    TARGET_LINK_LIBRARIES(timer_test timer )
    If you run this on Linux, it will generate a makefile which in turn will make an executable (timer_test) that loads a shared object (libtimer.so) and do something. On Windows it would create timer_test.exe and timer.dll. On Mac it would make timer_test and time.dynlib. Etc.

    The thing is, it builds a project that the native compiler could deal with.

    Bakefile acted similar...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, thanks.
    I'm going to use CMake. However, I can't figure out what to do after you write the CMakeLists file. I'm assuming you pass it to CMake, and it will generate the makefile, but I've already tried this and it gave some kind of error. I notice CMake has a --build option, but I don't how to use that. I tried passing that option, along with the filepath to the CMakeLists file, but that also didn't work. So I don't know what "<dir>" is for. Maybe its for the directory it builds a "project binary tree" (whatever that is) in.

    Also, I see in the tutorials and documentation that the ADD_EXECUTABLE variable can take multiple sources. But it doesn't mention headers, so I went and added them in there anyway. Or do you not have to explicitly list the headers of the program, as long as the headers are included in the sources?
    Last edited by Programmer_P; 06-10-2010 at 07:04 AM. Reason: changed add_executable to all CAPS

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't actually pass the file to cmake, it works a little like make this way -- on the level of the directory (so you cannot have more than one CMakeLists.txt).

    Just run:
    cmake /path/to/dir

    "dir" being where the CMakeLists.txt is. This will leave your platform specific makefiles in your current directory (so you usually want to run this in dir with "cmake ." or "cmake `pwd`" or whatever).

    You don't have to list the headers you've #included, I believe CMake deals with that stuff. Dead minimum CMakeLists.txt:
    Code:
    project (myproj)
    add_executable (whatever.exe mysource.cpp)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Yeah what MK said is correct: most of the time I just run cmake . (in the directory of the source/CMakeLists.txt file). Read the docs; there is also a decent book available for it from Amazon amongst other places but you should be able to pick up enough to make basic executables and shared/static libraries from reading online stuff. I have a little Python script I run when I need to generate a simple, basic project to test something out that generates the CMakeLists.txt file, some C++ source and builds the project...works for me anyways.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thanks guys.
    I'm getting farther now.
    Getting errors though in CMake. I'm going to have to resolve those somehow.
    I'll get back to you on how it goes.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Personally, I wouldn't suggest CMake. Instead, have a look at premake.

    Basically from the above, in premake it would be:
    Code:
    solution "Timer"
       language "C++"
       defines { "DEBUG" }
       flags { "Symbols" }
    
    project "timer"
       kind "SharedLib"
       files { "timer.cpp" }
    
    project "timer_test"
       kind "ConsoleApp"
       links { "timer" }
       files { "timer_main.cpp" }
    And since it's Lua, you have the power to do much more than the clumsy CMake syntax. At least bakefile gives you the chance to execute python. In premake4, there is also introductory support for cross-compiling.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, turns out the last problem was due to the wrong permissions being set to the cmake folder and its contents, for some reason. Since I didn't change that myself after I downloaded it, I'm thinking the file permissions of the .sh script was probably set as restricted to just root from the beginning, which might explain why it extracted its files with all the directories and ........ as also root. Or something...I don't know.

    Anyway, the problem I'm having now with cmake is its claiming one of the sources I specified in the ADD_EXECUTABLE variable doesn't exist, when I know it does.

    What is the proper way to specify a filepath to a source in the ADD_EXECUTABLE variable? I used the format: child_directory/sub-child_directory/etc...

    So obviously I'm using relative paths. Is this not allowed by cmake, and do I have to specify the full filepath?

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Nevermind. I figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler dependencies. What are they?
    By shrink_tubing in forum C++ Programming
    Replies: 5
    Last Post: 01-09-2010, 10:57 PM
  2. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  3. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  4. How to write a makefile
    By yuzhangoscar in forum Tech Board
    Replies: 3
    Last Post: 09-23-2008, 02:06 PM
  5. unix makefile won't work but works in Dev C++
    By jk1998 in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2007, 03:54 PM