如何通过cmake使用-fPIC编译jsoncpp

时间:2015-03-02 13:35:43

标签: c++ cmake makefile static-libraries

我希望使用jsoncpp-fPIC汇总cmake

在jsoncpp的网站上,它正在解释正常的编译方式,如下所示。

mkdir -p build/debug
cd build/debug
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..
make

如何在此步骤中使用-fPIC标志进行编译?

-------------------------------------------- - - - - - - -编辑 - - - - - - - - - - - - - - - - - - -----------------------

我假设我必须在-fPIC的makefile中添加一些行。

所以我用cmake添加makefile的内容。

如何更改-fPIC标志的makefile?

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =

.SUFFIXES: .hpux_make_needs_suffix_list

# Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E remove -f

# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/kim/jsoncpp-pre-C-11

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/kim/jsoncpp-pre-C-11/build/debug

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
    /usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target install
install: preinstall
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
    /usr/bin/cmake -P cmake_install.cmake
.PHONY : install

# Special rule for the target install
install/fast: preinstall/fast
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
    /usr/bin/cmake -P cmake_install.cmake
.PHONY : install/fast

# Special rule for the target install/local
install/local: preinstall
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..."
    /usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake
.PHONY : install/local

# Special rule for the target install/local
install/local/fast: install/local
.PHONY : install/local/fast

# Special rule for the target install/strip
install/strip: preinstall
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..."
    /usr/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake
.PHONY : install/strip

# Special rule for the target install/strip
install/strip/fast: install/strip
.PHONY : install/strip/fast

# Special rule for the target list_install_components
list_install_components:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Available install components are: \"Unspecified\""
.PHONY : list_install_components

# Special rule for the target list_install_components
list_install_components/fast: list_install_components
.PHONY : list_install_components/fast

# Special rule for the target rebuild_cache
rebuild_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
    /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# Special rule for the target test
test:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running tests..."
    /usr/bin/ctest --force-new-ctest-process $(ARGS)
.PHONY : test

# Special rule for the target test
test/fast: test
.PHONY : test/fast

# The main all target
all: cmake_check_build_system
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles/progress.marks
    $(MAKE) -f CMakeFiles/Makefile2 all
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
    $(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
    $(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
    $(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named jsoncpp_lib

# Build rule for target.
jsoncpp_lib: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 jsoncpp_lib
.PHONY : jsoncpp_lib

# fast build rule for target.
jsoncpp_lib/fast:
    $(MAKE) -f src/lib_json/CMakeFiles/jsoncpp_lib.dir/build.make src/lib_json/CMakeFiles/jsoncpp_lib.dir/build
.PHONY : jsoncpp_lib/fast

#=============================================================================
# Target rules for targets named jsoncpp_check

# Build rule for target.
jsoncpp_check: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 jsoncpp_check
.PHONY : jsoncpp_check

# fast build rule for target.
jsoncpp_check/fast:
    $(MAKE) -f src/jsontestrunner/CMakeFiles/jsoncpp_check.dir/build.make src/jsontestrunner/CMakeFiles/jsoncpp_check.dir/build
.PHONY : jsoncpp_check/fast

#=============================================================================
# Target rules for targets named jsoncpp_readerwriter_tests

# Build rule for target.
jsoncpp_readerwriter_tests: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 jsoncpp_readerwriter_tests
.PHONY : jsoncpp_readerwriter_tests

# fast build rule for target.
jsoncpp_readerwriter_tests/fast:
    $(MAKE) -f src/jsontestrunner/CMakeFiles/jsoncpp_readerwriter_tests.dir/build.make src/jsontestrunner/CMakeFiles/jsoncpp_readerwriter_tests.dir/build
.PHONY : jsoncpp_readerwriter_tests/fast

#=============================================================================
# Target rules for targets named jsontestrunner_exe

# Build rule for target.
jsontestrunner_exe: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 jsontestrunner_exe
.PHONY : jsontestrunner_exe

# fast build rule for target.
jsontestrunner_exe/fast:
    $(MAKE) -f src/jsontestrunner/CMakeFiles/jsontestrunner_exe.dir/build.make src/jsontestrunner/CMakeFiles/jsontestrunner_exe.dir/build
.PHONY : jsontestrunner_exe/fast

#=============================================================================
# Target rules for targets named jsoncpp_test

# Build rule for target.
jsoncpp_test: cmake_check_build_system
    $(MAKE) -f CMakeFiles/Makefile2 jsoncpp_test
.PHONY : jsoncpp_test

# fast build rule for target.
jsoncpp_test/fast:
    $(MAKE) -f src/test_lib_json/CMakeFiles/jsoncpp_test.dir/build.make src/test_lib_json/CMakeFiles/jsoncpp_test.dir/build
.PHONY : jsoncpp_test/fast

# Help Target
help:
    @echo "The following are some of the valid targets for this Makefile:"
    @echo "... all (the default if no target is provided)"
    @echo "... clean"
    @echo "... depend"
    @echo "... edit_cache"
    @echo "... install"
    @echo "... install/local"
    @echo "... install/strip"
    @echo "... list_install_components"
    @echo "... rebuild_cache"
    @echo "... test"
    @echo "... jsoncpp_lib"
    @echo "... jsoncpp_check"
    @echo "... jsoncpp_readerwriter_tests"
    @echo "... jsontestrunner_exe"
    @echo "... jsoncpp_test"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
    $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

1 个答案:

答案 0 :(得分:1)

您必须更改CMAKE_CXX_FLAGSCMAKE_CXX_FLAGS变量。您可以通过ccmakecmake-gui执行此操作(您必须先切换),或者您可以按照以下方式提供:

cmake -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..

不要费心更改Makefile,因为下次运行cmake时会覆盖它。

CMake 2.8.9中,您可以设置POSITION_INDEPENDENT_CODE属性 在目标上使所有后续目标独立。或者您可以在CMakeLists.txt

中设置全局变量
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..