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?
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.
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
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):
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.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 )
The thing is, it builds a project that the native compiler could deal with.
Bakefile acted similar...
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
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
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.
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.
Personally, I wouldn't suggest CMake. Instead, have a look at premake.
Basically from the above, in premake it would be:
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.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" }
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?
Nevermind. I figured it out.