作者需要创建目录,而且即要在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