Linux下运行.cpp文件

这篇博客介绍了如何在C++环境中编写简单的程序并进行编译执行,从基础的'hello,world'程序开始,逐步引入命令行参数。接着,讲解了如何使用CMake配置文件来构建一个OpenCV项目,包括设置OpenCV库、生成makefile和编译运行。通过这个过程,读者可以学习到C++程序的基本构建和OpenCV的初步应用。
摘要由CSDN通过智能技术生成

参考: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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值