如何在/ usr/local编译时使用clang ++进行链接?

问题描述:

我不是基于命令行编译的专业版。我从boost official example开发了以下的asio UDP应用程序。如何在/ usr/local编译时使用clang ++进行链接?

// udpServer.cpp 

#include <boost/asio.hpp> 
#include <iostream> 
#include <array> 

using boost::asio::ip::udp; 

int main() 
{ 
    try 
    { 
    boost::asio::io_service io_service; 

    udp::socket socket(io_service, udp::endpoint(udp::v4(), 13)); 

    for (;;) 
    { 
     std::array<char, 1> recv_buf; 
     udp::endpoint remote_endpoint; 
     boost::system::error_code error; 
     socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint, 0, error); 

     if (error && error != boost::asio::error::message_size) 
     throw boost::system::system_error(error); 

     std::string message = "some_string"; 

     boost::system::error_code ignored_error; 
     socket.send_to(boost::asio::buffer(message), remote_endpoint, 0, ignored_error); 
    } 
    } 

    catch (std::exception& e) 
    { 
    std::cerr << e.what() << std::endl; 
    } 

    return 0; 
} 

我使用Macports安装了1.59,因为这是我在做的版本sudo port install boost。我看到,升压位于我/usr/local/lib &头在/usr/local/include

我试图从其他讨论的建议,但代码不编译,因为它是不能够链接到提高。我在OSX &试图编译铿锵用下面的命令

clang++ -std=c++14 udpServer.cpp 

试过这种

clang++ -std=c++14 -I /usr/local/include/boost -L /usr/local/lib udpServer.cpp 

但得到以下错误:

Undefined symbols for architecture x86_64: 
"boost::system::system_category()", referenced from: 
    boost::asio::error::get_system_category() in udpServer-4b9a12.o 
    boost::system::error_code::error_code() in udpServer-4b9a12.o 
    ___cxx_global_var_init.2 in udpServer-4b9a12.o 
"boost::system::generic_category()", referenced from: 
    ___cxx_global_var_init in udpServer-4b9a12.o 
    ___cxx_global_var_init.1 in udpServer-4b9a12.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
+0

为什么不使用'自制'? – merito

试试这个:

clang++ -std=c++14 -I /usr/local/include -L /usr/local/lib -lboost_system udpServer.cpp -o updServer 

您将头文件包含为<boost/asio.hpp>,因此只需传递-I /usr/local/include即可。 -I-L只是让链接器知道哪里找到标题和库。您还需要让链接器知道您实际需要通过-l<library_name>链接的库。

顺便说一句,/usr/local/include是默认的标题搜索路径,/usr/local/lib是默认的库搜索路径。所以你可以:

clang++ -std=c++14 -lboost_system udpServer.cpp -o updServer 
+0

非常感谢。那是我需要的。了解现在如何工作。是的 –