12

我正在尝试将旧的 makefile 代码转换为 CMake。你能帮助我吗?这是我目前陷入困境的部分。我不知道如何将这些参数传递给编译器。

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

谢谢!

4

1 回答 1

19

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.

于 2013-05-20T15:01:26.830 回答