2015-03-02 292 views
0

我想编译jsoncpp-fPICcmake如何通过cmake与-fPIC编译jsoncpp

在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

所以我通过cmake添加makefile的内容。

我怎样才能改变这个生成文件为-fPIC国旗?

# 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 
+0

可能的全局变量复制http://stackoverflow.com/questions/18604301 – quimnuss 2015-03-02 14:34:47

回答

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 ..