【问题标题】:Add C++11 support to Makefile向 Makefile 添加 C++11 支持
【发布时间】:2014-04-04 13:14:02
【问题描述】:

我正在尝试在 Ubuntu 上使用 make 命令构建库,我收到以下错误消息:

In file included from /usr/include/c++/4.7/cstdint:35:0,
                 from /home/mohammad/face-analysis-sdk-stable/src/utils/helpers.hpp:26,
                 from /home/mohammad/face-analysis-sdk-stable/src/utils/command-line-arguments.cpp:21:
/usr/include/c++/4.7/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
make[2]: *** [src/utils/CMakeFiles/utilities.dir/command-line-arguments.cpp.o] Error 1
make[1]: *** [src/utils/CMakeFiles/utilities.dir/all] Error 2
make: *** [all] Error 2

我知道我需要在某处添加 C++11 标志,但我以前从未使用过 make 文件,我查看了 make 文件,但找不到应该在哪里添加它。 我找不到任何关于 g++ 或编译标志或任何东西的提及。

以下是make文件的一部分,请指点我应该编辑吗?

# 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/mohammad/face-analysis-sdk-stable

# The top-level build directory on which CMake was run. CMAKE_BINARY_DIR = /home/mohammad/face-analysis-sdk-stable/build

#=============================================================================
# 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 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

# The main all target all: cmake_check_build_system     $(CMAKE_COMMAND) -E cmake_progress_start /home/mohammad/face-analysis-sdk-stable/build/CMakeFiles /home/mohammad/face-analysis-sdk-stable/build/CMakeFiles/progress.marks   $(MAKE) -f CMakeFiles/Makefile2 all     $(CMAKE_COMMAND) -E cmake_progress_start /home/mohammad/face-analysis-sdk-stable/build/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 utilities

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

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

【问题讨论】:

  • 如果您想要完整的 C++11 支持,您需要一个 gcc 版本 >= 4.8.1,以前的版本仅对该版本的标准提供部分和不完整的支持。
  • 通常某处有一个CXX_FLAGS变量,你可以在其中添加-std=c++11
  • 如果您仍然可以访问它,您应该真正编辑CMakeFiles.txt 文件,或者只是更改与例如使用的CXXFLAGS ccmake。您发布的文件是生成的文件。

标签: c++ c++11 makefile


【解决方案1】:

您的 Makefile 已由 CMake 生成。

您应该在CMakeLists.txt 中激活C++11 的编译器选项。

我是这样做的:

SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )

当您生成解决方案时,它会简单地将正确的标志添加到您的 Makefile。

【讨论】:

  • 它通过了这一步,但现在产生了一些编译错误,这正常吗?我正在编译一个库,这应该是经过测试的发布版本
  • @Mhd.Tahawi 如果没有看到编译错误,我不能说这是否正常。它与 c++11 有关吗?图书馆是什么?
  • CI2CV 人脸库,感谢您的回复,如果需要,我会深入研究并发布一个新问题。感谢您的回答,这是一个救生员!
  • @Mhd.Tahawi 不客气 :) 我认为这是最好的解决方案。首先尝试看看您是否可以修复这些错误,如果您遇到困难,请发布一个新问题。
  • 我会使用set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 来避免覆盖可能已经设置的标志。我会使用系统默认的标准库,除非有特殊需要使用另一个。
【解决方案2】:

看起来您的 Makefile 是由 CMake 生成的。因此,要使用 C++11 标准编译,请将此行添加到 CMakeLists.txt

add_definitions(-std=c++0x)

【讨论】:

  • add_definitions 仅用于预处理器定义。我不知道将它用于其他标志是否有缺点。
猜你喜欢
  • 2012-11-29
  • 2017-09-11
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
相关资源
最近更新 更多