Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting old makefile to CMake

I'm trying to convert my old makefile code to CMake. Can you help me? This is the part where I'm currently stuck. I don't know how to pass these arguments to the compiler.

COMPILE_FLAGS = -c -m32 -O3 -fPIC -w -DSOMETHING -Wall -I src/sdk/core

ifdef STATIC
    OUTFILE = "bin/test_static.so"
    COMPILE_FLAGS_2 = ./lib/ABC.a
else
    OUTFILE = "bin/test.so"
    COMPILE_FLAGS_2 = -L/usr/lib/mysql -lABC
endif

all:
    g++ $(COMPILE_FLAGS) src/sdk/*.cpp
    g++ $(COMPILE_FLAGS) src/*.cpp
    g++ -fshort-wchar -shared -o $(OUTFILE) *.o $(COMPILE_FLAGS_2)
    rm -f *.o

Thank you!

like image 678
TheBlackTiger Avatar asked May 20 '13 13:05

TheBlackTiger


1 Answers

Let's try to map Makefile syntax to CMake:

COMPILE_FLAGS = -c -m32 -O3 -fPIC -w -DSOMETHING -Wall -I src/sdk/core

This statement directly maps to:

SET( COMPILE_FLAGS "-c -m32 -O3 -fPIC -w -DSOMETHING -Wall" )
INCLUDE_DIRECTORIES( src/sdk/core )

A conditional of the type:

ifdef STATIC
  # Do something
else
  # Do something else
endif

is translated in CMake in this way:

OPTION(STATIC "Brief description" ON)
IF( STATIC )
  # Do something
ELSE()
  # Do something else
ENDIF()

To modify the default compilation flags you can set the variables CMAKE_<LANG>_FLAGS_RELEASE, CMAKE_<LANG>_FLAGS_DEBUG, etc. , appropriately.

Finally the compilation of an executable requires you to use the ADD_EXECUTABLE command, which is explained in many CMake tutorials.

In any case I suggest you to refer to the online documentation for further details, as it is quite explanatory and complete.

like image 186
Massimiliano Avatar answered Oct 05 '22 04:10

Massimiliano