Compiling with g++

本文档简要介绍了如何使用g++编译基本的C++程序,包括使用make、UNIX、GDB和Emacs。g++是GNU C++编译器,通过-I指定头文件目录,-o指定输出文件名,-c生成对象文件,-L指定库目录,-l链接库。同时,文章提到了编译多文件程序的方法和一些常用的编译选项,如-g开启调试,-Wall开启所有警告,-O或-O2开启优化。

Compiling with g++

reference : https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html
there are some exciting things in it.

  • using make
  • using UNIX
  • using GDB
  • using Emacs

Executive Summary: This document is a brief description of how to compile basic C++ programs using g++. It provides sample command lines for invoking the g++ compiler and a list of some common compiler options.

What is g++?

g++ is your friendly Gnu C++ compiler. g++ does not handle templates well, but you can use them. This document serves as a very simple bunch of hints to start using g++, and is not meant to be complete. For all the gory details about g++'s options, check out its man page.
Compiling HelloWorld.C
Say you have a file helloworld.C as follows :

#include <stdio.h>

void main (){
    printf("Hello World\n");
}

You can compile and run it from the unix prompt as follows :

% g++ helloworld.C

This creates an executable called “a.out”. You can run it by typing

% ./a.out

Since no executable name was specified to g++, a.out is chosen by default. Use the “-o” option to change the name :

% g++ -o helloworld helloworld.C

creates an executable called “helloworld”.

Include Directories

Sometimes the header files that you write are not in the same directory as the .C file that #include’s it. For example you might have a a file “foo.h” that resides in /homes/me/randomplace/include. If you want to include that file in helloworld.C, you could just give the full path name in the #include, OR you can do the following:
Add
#include <foo.h>
to helloworld.C and compile it with the -I option :

% g++ -o helloworld -I/homes/me/randomplace/include helloworld.C 

This basically tells g++ to look for #include’s in /homes/me/include in addition to other directories you specify with -I

Compiling multiple files

Most likely, you will be compiling separate modules and linking them into a single executable. Here’s the basic idea: compile each .C file into a .o file, then link the .o files (along with any libraries) into an executable. Of course, one of these .C files has to define the main() or else the linker will complain. Suppose we have main.C, foo.C and bar.C and want to create an executable fubar, and suppose further that we need the math library:

% g++ -c -o foo.o foo.C 
% g++ -c -o main.o main.C
% g++ -c -o bar.o bar.C
% g++ -o fubar foo.o main.o bar.o -lm

The first three commands generate foo.o, main.o and bar.o respectively. The last line links them together along with the math library, libm.a.

Some options

-g - turn on debugging (so GDB gives more friendly output)
-Wall - turns on most warnings
-O or -O2 - turn on optimizations
-o - name of the output file
-c - output an object file (.o)
-I - specify an include directory
-L - specify a lib directory
-l - link with library lib.a

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值