【问题标题】:Compiling library in g++ using C++11使用 C++11 在 g++ 中编译库
【发布时间】:2016-02-28 10:44:35
【问题描述】:

几天来,我一直在尝试在我的 mac 上编译一个开源 C++ 库 (QuantLib-1.7),但我似乎遇到了某种 C++11 兼容性问题。

当我从终端运行 make && sudo make install 时,编译似乎可以正常工作,除了一堆表单错误

Making all in BermudanSwaption
g++ -DHAVE_CONFIG_H -I. -I../../ql  -I../.. -I../.. -I/opt/local/include  -g -O2 -MT BermudanSwaption.o -MD -MP -MF .deps/BermudanSwaption.Tpo -c -o BermudanSwaption.o BermudanSwaption.cpp
In file included from BermudanSwaption.cpp:22:
In file included from ../../ql/quantlib.hpp:43:
In file included from ../../ql/experimental/all.hpp:25:
In file included from ../../ql/experimental/volatility/all.hpp:21:
In file included from ../../ql/experimental/volatility/zabr.hpp:31:
In file included from ../../ql/math/statistics/incrementalstatistics.hpp:35:
In file included from /opt/local/include/boost/accumulators/statistics/stats.hpp:14:
In file included from /opt/local/include/boost/accumulators/statistics_fwd.hpp:12:
/opt/local/include/boost/mpl/print.hpp:50:19: warning: in-class initialization
  of non-static data member is a C++11 extension [-Wc++11-extensions]
const int m_x = 1 / (sizeof(T) - sizeof(T));
              ^
1 warning generated.

我猜这与 g++ 没有为 C++11 正确配置有关。我熟悉可以通过使用g++ -std=c++11 编译来调用 C++11 的事实。但是,尽管进行了很多谷歌搜索,但我找不到修改 makefile 的方法,以便在运行 make && sudo make install 时调用 -std=c++11

任何帮助将不胜感激。

这是我认为相关的 makefile 部分:

BOOST_INCLUDE = -I/opt/local/include
BOOST_LIB = -L/opt/local/lib
BOOST_THREAD_LIB = 
BOOST_UNIT_TEST_DEFINE = -DQL_WORKING_BOOST_STREAMS
BOOST_UNIT_TEST_LIB = boost_unit_test_framework-mt
BOOST_UNIT_TEST_MAIN_CXXFLAGS = -DBOOST_TEST_DYN_LINK
CC = gcc
CCDEPMODE = depmode=gcc3
CFLAGS = -g -O2
CPP = gcc -E
CPPFLAGS =  -I/opt/local/include
CXX = g++
CXXCPP = g++ -E
CXXDEPMODE = depmode=gcc3
CXXFLAGS = -g -O2

这是运行“g++ -v”的输出:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

Makefile.am:https://www.dropbox.com/s/v5j7qohwfup81od/Makefile.am?dl=0

Makefile.in:https://www.dropbox.com/s/t92hft9ea2ar1zw/Makefile.in?dl=0

QuantLib-1.7 目录:https://www.dropbox.com/sh/ulj0y68m8x35zg8/AAA-w7L2_YWIP8_KnwURErzYa?dl=0

完整的错误日志:https://www.dropbox.com/s/g09lcnma8skipv7/errors.txt?dl=0

【问题讨论】:

  • 为什么不brew install quantlib
  • 好的,现在试一试。
  • g++ -I/opt/local/include/ -I/opt/local/include/boost BermudanSwaption.cpp \ > -o bermudanswaption -L/opt/local/lib/ -lQuantLib 后跟 ./bermudanswaption 现在似乎运行成功。这一切似乎都很混乱。有没有什么地方可以读到关于 quintib 的头文件和 cpp 文件是如何在文件系统中组织的?谢谢。
  • 为什么不直接编辑显示“CXX = g++”的行使其显示为“CXX = g++ -std=c++11”?

标签: macos c++11 gcc makefile


【解决方案1】:

添加类似的东西

CXXFLAGS += -std=c++11

到您的Makefile。无论g++ 可执行文件的特定于达尔文的修改如何,这都将起作用——它实际上是clang++

参考资料:

【讨论】:

  • 嗨 rubicks。我实际上尝试将CXXFLAGS += -std=c++11 添加到无扩展名makefile 中,但这不起作用。我尝试将行添加到 Makefile.am 导致以下错误:cd . && /bin/sh /Users/james/Dropbox/Finance/QuantLib-1.7/config/missing automake-1.14 --foreign Makefile /Users/james/Dropbox/Finance/QuantLib-1.7/config/missing: line 81: automake-1.14: command not found WARNING: 'automake-1.14' is missing on your system.
  • @user11881 好的,这改变了一切。如果您已经有自动工具,则不建议编辑任何生成的Makefile。相反,请尝试将 AM_CXXFLAGS += -std=c++11 添加到您的 Makefile.am
  • @user11881 ,如果您缺少最新的稳定自动工具,也可以获取它们:brew install autoconf automake libtool autoconf-archive
  • 好的,运行 brew install autoconf automake libtool autoconf-archive,将 M_CXXFLAGS += -std=c++11 添加到 Makefile.am,但我仍然收到 cd . && /bin/sh /Users/james/Dropbox/Finance/QuantLib-1.7/config/missing automake-1.14 --foreign Makefile /Users/james/Dropbox/Finance/QuantLib-1.7/config/missing: line 81: automake-1.14: command not found WARNING: 'automake-1.14' is missing on your system.
  • You should only need it if you modified 'Makefile.am' or 'configure.ac' or m4 files included by 'configure.ac'. The 'automake' program is part of the GNU Automake package: <http://www.gnu.org/software/automake> It also requires GNU Autoconf, GNU m4 and Perl in order to run: <http://www.gnu.org/software/autoconf> <http://www.gnu.org/software/m4/> <http://www.perl.org/>
【解决方案2】:

正如您已经拥有并且熟悉homebrew,我的建议是使用它来安装和管理quantlib,如下所示:

brew install quantlib

然后将构建所有文件并将/usr/local/Cellar/quantlib 中的所有文件放在某个不重要的版本号下。重要的是这些工具随后会链接到 /usr/local/bin,因此您需要做的就是确保 /usr/local/bin 在您的 PATH 中。

这使您可以访问工具quantlib-config,该工具始终链接到最新版本,并且知道最新版本。所以,如果你运行:

quantlib-config --cflags

它会告诉你includes 的正确路径是这样的:

-I/usr/local/Cellar/quantlib/1.6.1/include

同样,如果你运行:

quantlib-config --libs

它会告诉你最新版本的正确链接目录和库。

简而言之,编译所需要做的就是:

g++ $(quantlib-config --cflags --libs)

它总是会拉入您正在使用的版本。

请注意,如果您使用 Makefile,则需要将美元符号加倍。

【讨论】:

  • 嗨,马克。如果我没记错的话,您的答案是指使用从 brew 下载的预编译 quantlib 二进制包编译示例文件。实际上,我对出于开发目的编译 quantlib 库更感兴趣。
【解决方案3】:

这就是我最终设法编译 Quantlib 库以供将来参考的方式。这可能不是最有效/最优雅的方法,但似乎很有效。

  1. 按照http://quantlib.org/install/macosx.shtml给出的步骤,发现运行make && sudo make install导致OP报错。

  2. 在 Eclipse 中创建一个名为“Quantlib”的新静态库 C++ 项目

  3. 将 .tar 文件中的 ql 目录复制到 Quantlib Eclipse 工作区

  4. 右键单击 Quantlib > 属性 > C/C++ 构建 > 设置 > Cross G++ 编译器:将语言标准更改为 ISO C++ 11 (-std=c++0x)

  5. 右键单击 Quantlib > C/C++ 常规 > 路径和符号:为 GNU C++ 添加以下包含目录

    选择/本地/包含

    /Quantlib(勾选“是工作区目录”)

    /opt/local/include/boost.

  6. 构建 Quantlib 项目(在 MacBook Air 1.8 GHz Intel Core i7 上大约需要 34 分钟)

  7. 创建一个新的 C++ 可执行项目(例如 BermudanSwaption)并将 BermudanSwaption.cpp 复制到 BermudanSwaption Eclipse 工作区

  8. 对 BermudanSwaption Eclipse 项目重复步骤 4. 和 5.

  9. 右键单击 BermudanSwaption > Properties > C/C++ General > Paths and Symbols > References:检查 Quantlib(Library Paths 选项卡现在应该包含条目“/Quantlib/Debug”)

  10. 构建并运行 BermudanSwaption 可执行项目


QuantLib-1.7

OSX 优胜美地 10.10.5

Eclipse C/C++ 开发工具版本:8.8.0.201509131935

Xcode 版本 7.1 (7B91b)

xcode-选择版本 2339。

【讨论】:

  • 附加说明:我发现对于 Quantlib 静态项目,选择编译器作为“Cross GCC”很重要。如果我改为选择“MacOSX GCC”,我发现每次尝试编译可执行项目时整个库都会重新编译(我为可执行项目选择了“MacOSX GCC”)
猜你喜欢
  • 2012-05-08
  • 2020-03-07
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
相关资源
最近更新 更多