why CLion?
CLion有众多好处,如Smart C和C ++编辑器、导航、重构、实时代码分析等,有跨平台IDE中的百兽之王之称。
我准备使用CLion的主要理由有两点:
其一,我一直使用JetBrains的IDE,如IntelliJ IDEA(java开发),pycharm(python开发),熟悉该公司的IDE,方便快速开发,专注于代码本身。
其二,CLion使用CMake作为项目模型,CLion会自动处理您在CMake文件中的更改,使用代码编辑和重构期间的所有信息。现在写C++项目基本要用的CMake,CLion中写文件非常简单方便,有智能提示,比用vim手写方便多了。CMake参考资料。
CLion 添加第三方库
以opencv为例:
前提是按照opencv官方已经正确安装opencv.
方式一:
-
cmake_minimum_required(VERSION 3.6)
-
project(hello)
-
set(CMAKE_CXX_STANDARD 11)
-
find_package(OpenCV REQUIRED)
-
-
set(SOURCE_FILES )
-
add_executable(hello ${SOURCE_FILES})
-
target_link_libraries(hello ${OpenCV_LIBRARIES})
-
cmake_minimum_required(VERSION 3.6)
-
project(hello)
-
set(CMAKE_CXX_STANDARD 11)
-
-
set(SOURCE_FILES )
-
add_executable(hello ${SOURCE_FILES})
-
# 第三方库位置可以通过命令pkg-config --cflags --libs opencv查看
-
target_link_libraries(hello -I/usr/local/include/opencv -I/usr/local/include
-
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d
-
-lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy
-
-lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo
-
-lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
-
-lrt -lpthread -lm -ldl)
pkg-config --cflags opencv:
-I/usr/local/include/opencv -I/usr/local/include
pkg-config --libs opencv:
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
pkg-config --cflags --libs opencv
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
对比gcc的 -I 参数 去哪儿找头文件 .h
对比gcc的 -L 参数 去哪儿找库文件 (.so/.dll/.lib/.dylib/...)
对比gcc的 -l 参数 需要链接的库文件的名字
两种截然不同的开发体验
also:
g++ -o xu_opencv_test xu_opencv_test.cpp `pkg-config --cflags --libs opencv`