2017-04-04 214 views
0

我有一个链接问题,当我试图从boost使用文件系统。我复制代码教程:链接错误使用boost库和clion

#include <iostream> 
#include <boost/filesystem.hpp> 
using namespace boost::filesystem; 

int main(int argc, char* argv[]) 
{ 
    if (argc < 2) 
    { 
     std::cout << "Usage: tut1 path\n"; 
     return 1; 
    } 
    std::cout << argv[1] << " " << file_size(argv[1]) << '\n'; 
    return 0; 
} 

或许它应该工作,但我有这样的错误:

Scanning dependencies of target untitled 
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o 
[100%] Linking CXX executable untitled 
Undefined symbols for architecture x86_64: 
    "boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)", referenced from: 
     boost::filesystem::file_size(boost::filesystem::path const&) in main.cpp.o 
    "boost::system::system_category()", referenced from: 
     ___cxx_global_var_init.2 in main.cpp.o 
    "boost::system::generic_category()", referenced from: 
     ___cxx_global_var_init in main.cpp.o 
     ___cxx_global_var_init.1 in main.cpp.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
make[3]: *** [untitled] Error 1 
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2 
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2 
make: *** [untitled] Error 2 

我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.7) 
project(untitled) 

set(CMAKE_CXX_STANDARD 11) 

set(SOURCE_FILES main.cpp) 
add_executable(untitled ${SOURCE_FILES}) 

find_package(Boost) 
IF (Boost_FOUND) 
    include_directories(${Boost_INCLUDE_DIR}) 
endif() 

我能提高什么编译这个代码?我在OSX上使用CLion编辑器。

+0

请向我们展示您的make make/makefile/CMakeLists.txt! – Rama

+0

'cmake_minimum_required(3.7版) 项目(无) 集(CMAKE_CXX_STANDARD 11) 集(SOURCE_FILES main.cpp中) add_executable(无$ {} SOURCE_FILES) find_package(升压) IF(Boost_FOUND) include_directories ($ {Boost_INCLUDE_DIR}) endif()' 对不起,这种粘贴格式 - 我不知道如何使它成为多行。 – emil

+0

@emil你将要把它附加到你的问题,而不是留在评论中。 – chbchb55

回答

0

更改这一行你CMakeList.txt:

find_package(Boost) 

通过

find_package(Boost COMPONENTS system filesystem REQUIRED) 

,不要忘记你的目标链接:

target_link_libraries(untitled 
    ${Boost_FILESYSTEM_LIBRARY} 
    ${Boost_SYSTEM_LIBRARY} 
) 

所以你CMakeList。 txt将是:

cmake_minimum_required(VERSION 3.7) 
project(untitled) 

set(CMAKE_CXX_STANDARD 11) 

set(SOURCE_FILES main.cpp) 
add_executable(untitled ${SOURCE_FILES}) 

find_package(Boost COMPONENTS system filesystem REQUIRED) 
IF (Boost_FOUND) 
    include_directories(${Boost_INCLUDE_DIR}) 
    target_link_libraries(untitled 
     ${Boost_FILESYSTEM_LIBRARY} 
     ${Boost_SYSTEM_LIBRARY} 
     ) 
endif()