2017-05-06 39 views
0

我不是基於命令行編譯的專業版。我從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

回答

1

試試這個:

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

非常感謝。那是我需要的。瞭解現在如何工作。是的 –

相關問題