参考:https://www.cnblogs.com/1996313xjf/p/5876637.html
一、写一个保存为abc.cpp
然后在终端
g++ -o abc abc.cpp
./abc
abc.cpp
#include<iostream>
using namespace std;
int main()
{
cout<<"hello, world"<<endl;
}
添加输入:
#include<iostream>
using namespace std;
int main(int argc, char *agrv[])
{
cout<<"hello, world"<<endl;
}
二、写一个用opencv的cpp
参考:https://blog.csdn.net/m0_37357063/article/details/84191669
写一个CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
#这是对CMake工具最低版本要求,这里我们要检查下我们的CMake工具的版本信息,我们可以使用命令“cmake --version”查看
# Define project name
project(opencv_example)
#//这是建立一个工程项目(类似于我们VS中建立C++项目一样),括号里面时工程名,工程名我们可以任意给,最后程序编译出来的可执行文件就是这个名字
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED) #这是cmake用来查找opencv包用的,
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(abc abc.cpp) #这里括号里面的两个参数分别是工程项目名和我们要编译文件名的意思,记住中间一空格键隔开
# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS}) #这是我们链接到OpenCV库的环节,我们只要更改前面第一个参数为我们的工程项目名即可
然后生成makefile文件,再编译文件。
cmake .
make
./abc