Converting old makefile to CMake

2020-05-21 06:13发布

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!

1条回答
【Aperson】
2楼-- · 2020-05-21 07:04

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.

查看更多
登录 后发表回答