SSteve 之前的回复很棒,也帮助我整理了 CodeRunner 中的链接 Boost 库。
由于上一个回复中的解决方案是针对OpenCV库的,一般情况下,不小心将外部库添加到clang++命令行中可能只会产生大量的构建错误,我尝试时就是这种情况链接 Boost 库。
在这里,我想澄清一下 SSteve 回复中不清楚的地方,以便每个人都知道在 Mac OS 系统中使用外部库编译代码之前如何以及在何处修改命令行。
我会用我的案例来解释,但在某些时候我会告诉你 CodeRunner 设置或一般命令行输入中的棘手部分。
我使用macport 来安装Boost 库
sudo port install boost
- 头文件位于
/opt/local/include
- 库位于
/opt/local/lib/
如果您在 Boost 中找不到特定的子库,请打开终端并输入
cd /opt/local/lib/
find . -iname "*boost*"
您应该看到 Boost 的所有子库(静态库以 .a 结尾,动态库以 .dylib 结尾)如下所示。
在开始之前修改原来的命令行(支持c++ 14版本)比如
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" "${files[@]}" "${@:1}" ${CR_DEBUGGING:+-g}
你需要知道头文件的目录在-I之后,Boost库的目录在-L之后,比如
-I /opt/local/include/
-L /opt/local/lib/
为了在Boost中使用编译的静态或动态子库(见上图),你必须在-L /opt/local/lib/之后专门包含它。但是,简单地复制库名称而不带文件扩展名 .a 或 .dylib 永远不会让 CodeRunner 找到您希望运行的库!!!
详细解释here,我只是引用下面的重要部分
clang -dynamiclib -o libtest.dylib file1.o file2.o -L/some/library/path -lname_of_library_without_lib_prefix
在Boost Quickstart Document中运行这样的示例代码
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
现在包含<boost/regex.hpp> 的方法是通过
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" -I /opt/local/include/ -L /opt/local/lib -lboost_regex-mt "${files[@]}" "${@:1}" ${CR_DEBUGGING:+-g}
通过使用此命令行,您应该能够使用 Boost 库编译示例代码。
只需记住将前缀-lib 替换为-l 并在命令行中排除文件扩展名。
最后,有一些替代解决方案可以使用 Xcode 包含外部库,位于here