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

标签 c++ cmake makefile static-libraries

我想通过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 标志进行编译?

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

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

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

我如何更改此 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

最佳答案

您必须更改 CMAKE_CXX_FLAGSCMAKE_C_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 ..

关于c++ - 如何通过cmake使用-fPIC编译jsoncpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28811130/

相关文章:

linux - shell 和 makefile 之间的公共(public) "include"文件

c++ - qt 文件读取行总是在第二次读取时返回 -1

c++ - 哪些工具可以报告此类未初始化变量的使用情况?

c++ - 为什么这个 cmake 初始缓存文件会导致这种奇怪的错误?

android - 生成 Android.mk 的工具

c++ - g++ Windows 链接器 "Undefined reference to..."与 SDL2

c++ - QSpinBox 中以编程方式 "select all"

c++ - L2数据和指令缓存突然减少

c++ - 找不到 LMDB(缺少 : LMDB_INCLUDE_DIR LMDB_LIBRARIES)

c++ - CMake 找到了 Boost 但 Make 找不到包含文件