2017-04-04 2 views
0

Ich habe ein Verknüpfungsproblem, wenn ich versuche, Dateisystem von Boost zu verwenden. Ich kopierte Code aus Tutorial:Verknüpfen mit Fehler mit Boost-Bibliothek und 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; 
} 

Vielleicht sollte es funktionieren, aber ich habe einen solchen Fehler:

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 

Meine CMakeLists.txt Datei:

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() 

Was kann ich verbessern diesen Code kompilieren? Ich benutze CLion Editor auf OSX.

+0

Zeigen Sie uns bitte Ihre make comand/makefile/CMakeLists.txt bitte! – Rama

+0

'cmake_minimum_required (VERSION 3.7) Projekt (ohne Titel) Satz (CMAKE_CXX_STANDARD 11) Satz (SOURCE_FILES main.cpp) add_executable (untitled $ {SOURCE_FILES}) find_package (Boost) IF (Boost_FOUND) include_directories ($ {Boost_INCLUDE_DIR}) endif() ' Entschuldigung für dieses Einfügeformat - ich weiß nicht, wie man es multiline macht. – emil

+0

@emil werden Sie das an Ihre Frage anhängen wollen, anstatt es in den Kommentaren zu lassen. – chbchb55

Antwort

0

Ändern Sie diese Zeile in Ihrer CMakeList.txt:

find_package(Boost) 

von

find_package(Boost COMPONENTS system filesystem REQUIRED) 

Und vergessen Sie nicht mit Ihrem Ziel zu verknüpfen:

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

So Ihre CMakeList. txt wird sein:

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()