码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 十三、ROS中的头文件与源文件,Python模块导入


    文章目录

    • 1、设计头文件,可执行文件本身作为源文件
      • 1. 编写头文件
      • 2. 配置c_cpp_propertise.json文件中的includepath属性
      • 3.编写可执行文件
      • 4. 配置CMakeLists.txt文件
      • 5.编译执行
    • 2、设计头文件,可执行文件与源文件分离
      • 1. 编写头文件
      • 2. 配置c_cpp_propertise.json文件中的includepath属性
      • 3. 编写源文件
      • 4. 编写可执行文件
      • 5. 配置CMakeLists.txt文件
      • 6. 编译执行
    • 3、Python模块导入
      • 1. 编写工具模块
      • 2.编写主程序
      • 3.CMakeLists.txt文件配置
      • 4.编译执行

    1、设计头文件,可执行文件本身作为源文件

    1. 编写头文件

    在功能包下的include/功能包名 目录下新建hello1.h头文件

    #ifndef _HELLO_H
    #define _HELLO_H
    
    namespace hello_ns{
        class HelloPub{
            public:
            void run();
        };
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 配置c_cpp_propertise.json文件中的includepath属性

    需要加入自己编写的头文件路径

    "/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05/src/head_test/include/**"
    
    • 1

    3.编写可执行文件

    #include"ros/ros.h"
    #include"head_test/hello1.h"
    
    namespace hello_ns{
        void HelloPub::run(){
            ROS_INFO("hello1.h头文件");
        }
    }
    
    int main(int argc, char *argv[]){
        setlocale(LC_ALL,"");
        ros::init(argc,argv,"hello1");
        hello_ns::HelloPub helloPub;
        helloPub.run();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4. 配置CMakeLists.txt文件

    • 头文件配置
      include_directories(
      include
      ${catkin_INCLUDE_DIRS}
      )
      
      • 1
      • 2
      • 3
      • 4
    • 可执行文件配置
      add_executable(hello1 src/hello1.cpp)
      
      add_dependencies(hello1 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
      
      target_link_libraries(hello1
      ${catkin_LIBRARIES}
      )
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    5.编译执行

    2、设计头文件,可执行文件与源文件分离

    1. 编写头文件

    #ifndef _HELLO_H
    #define _HELLO_H
    
    namespace hello_ns1{
        class HelloPub1{
            public:
            void run1();
        };
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 配置c_cpp_propertise.json文件中的includepath属性

    需要加入自己编写的头文件路径

    "/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05/src/head_test/include/**"
    
    • 1

    3. 编写源文件

    #include"ros/ros.h"
    #include"head_test/hello2.h"
    
    namespace hello_ns1{
        void HelloPub1::run1(){
            ROS_INFO("hello2.h");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4. 编写可执行文件

    #include"ros/ros.h"
    #include"head_test/hello2.h"
    
    int main(int argc,char *argv[]){
        ros::init(argc,argv,"hello2_use");
        hello_ns1::HelloPub1 my;
        my.run1();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5. 配置CMakeLists.txt文件

    • 头文件和源文件相关配置
      include_directories(
      include
      ${catkin_INCLUDE_DIRS}
      )
      
      
      ## Declare a C++ library
      add_library(hello2      #hello2:名字随意,不作要求
      include/head_test/hello2.h  #头文件目录
      src/hello2.cpp  #源文件目录
      )
      
      #此处hello2对应上面的hello2,注意此处add_dependencise的位置
      ## Declare a C++ executable
      ## With catkin_make all packages are built within a single CMake context
      ## The recommended prefix ensures that target names across packages don't collide
      add_dependencies(hello2 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
      
      #此处hello2对应上面的hello2
      target_link_libraries(hello2
      ${catkin_LIBRARIES}
      )
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 可执行文件相关配置
    add_executable(hello2_use src/hello2_use.cpp)
    
    #注意此处add_denpendencies的位置
    ## Add cmake target dependencies of the executable
    ## same as for the library above
    add_dependencies(hello2_use ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
    
    target_link_libraries(hello2_use
      hello2
      ${catkin_LIBRARIES}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6. 编译执行

    3、Python模块导入

    1. 编写工具模块

    #! /usr/bin/env python
    
    num = 1000
    
    • 1
    • 2
    • 3

    2.编写主程序

    import os 
    import sys
    import rospy
    '''
        在Python中,一个文件引用另一个文件中的变量,
        只需保证二者在同一个文件夹下即可,Python会默
        认从当前文件夹搜索相关文件,但是,在ROS中,他
        会先搜索根目录下的文件,所以,为了顺利运行程序,
        需加入相应的搜索路径和优先级
    '''
    path = os.path.abspath(".")#获取该项目的绝对路径
    
    sys.path.insert(0,path+"/src/head_test/scripts")#加入文件搜索路径,0的优先级最高
    
    import tools
    if __name__ == "__main__":
        rospy.init_node("head_p")
        rospy.loginfo("path:%s",path)
        rospy.loginfo("num=%d",tools.num)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.CMakeLists.txt文件配置

    catkin_install_python(PROGRAMS
      scripts/main.py
      DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
    )
    
    • 1
    • 2
    • 3
    • 4

    4.编译执行

  • 相关阅读:
    【文档智能】:GeoLayoutLM:一种用于视觉信息提取(VIE)的预训练模型
    iShot——Mac上功能最全的截图、录屏创造工具
    固体物理概念
    Node.js 知识整理之第三篇(Express中间件)
    为什么使用KT6368A蓝牙芯片用app连接,基本都在5分钟左右后断开
    .net 6 api 修改URL为小写
    基于Java+控制台实现教材管理系统
    oracle的redo与postgreSQL的WAL以及MySQL的binlog区别
    SpringMVC第六阶段:数据在域中的保存(02)
    k8s部署禅道
  • 原文地址:https://blog.csdn.net/qq_43280851/article/details/125436070
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号