用Boost库创建目录

作者需要创建目录,而且即要在win和linux下都创建,而且用C++开发,这样就需要使用Boost库。

在安装好Boost库后,编写主函数


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

int main(){
    boost::filesystem::path path("/home/sys-01/pro_hw/lr");
    if (!boost::filesystem::is_directory(path))
    {
        cout << "begin create path: " << path << endl;
        if (!boost::filesystem::create_directory(path))
        {
            cout << "create_directories failed: " << path << endl;
            return -1;
        }
    }
    else
    {
        cout << path << " aleardy exist" << endl;
    }
}

接着编写CMakeLists.txt(直接用g++也行)要注意的是,虽然上面boost只include了filesystem文件,但链接的时候必须再链接boost_system.so。

cmake_minimum_required(VERSION 2.8)
project(Test)
add_definitions(-w)
add_definitions(-std=c++11)
#set(CMAKE_CXX_FLAGS "-Wall")



include_directories(/usr/local/include)
link_directories(/usr/local/lib)
add_executable(main main.cpp)
target_link_libraries(main boost_system.so libboost_filesystem.so)

编译后运行即可创建目录(自己运行时需要修改Path为自己的目录)

如果没有链接 boost_system.so,会出现以下问题:

sys-01@ubuntu:~/pro_hw/test/build$ make
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: undefined reference to symbol '_ZN5boost6system16generic_categoryEv'
/usr/local/lib/libboost_system.so.1.67.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:96: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值