简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM