티스토리 뷰

간단한 명령어를 통해 ros2 package를 만들 수 잇다.

$ ros2 pkg create ${package-name}

${package-name}에 내가 만들고 싶은 패키지 이름을 넣으면 패키지가 만들어 지는데

패키지는 다음과 같은 파일들이 존재하게 된다.

${package-name}/src : 소스 폴더

${package-name}/include :  include 폴더

${package-name}/CMakeLists.txt : project의 CMakelists

${package-name}/package.xml : project의 package 에 대한 정보와 dependency 정보를 포함하고 있다.


package.xml

package.xml에는 우리가 사용할 dependency 파일들 

예를들면 CMakeLists.txt 에서 find_package로 찾아서 쓰는 package들을 dependency에 추가해줘야한다.

rclcpp같은경우를 

<depend> rclcpp </depend> 로 dependency에 대한 패키지 정보를 담고 있다. 이를 가지고 있지 않으면 빌드가 원할히 진행되지 않는다.

 


CMakeLists.txt 

executable을 추가하거나 library를 추가 해줄때 해야할게 가장 많은 것이 CMakeLists이다. 

https://www.tuwlab.com/ece/27234

https://www.tuwlab.com/ece/27260

위 두 링크에서 CMakeLists에 대한 주요 명령과 변수가 잘 정리 되어있다. 개인적으로는 CMakeLists를 만드는것에 아직 익숙하지 않기 때문에 자주 참고하여 코드를 짜는 편이다.

간단한 executable 예재로 설명 해 보려 한다. 전체적인 코드는 다음과 같다.

cmake_minimum_required(VERSION 3.5)
project(rock)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

include_directories(include 
)
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

add_executable(main_exe src/main.cpp)
ament_target_dependencies(main_exe rclcpp)
target_link_libraries(main_exe )

install(TARGETS 
  main_exe
  DESTINATION lib/${PROJECT_NAME}
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

한부분씩 아는대로 적어보자면

// ---- CMake의 minimum version 을 정함 ----
cmake_minimum_required(VERSION 3.5)

// ---- 프로젝트 이름을 선언 해당 이름을 CMakeLists 내에 ${Proejct_name} 으로  접근가능 ----
project(rock)

// ---- CMake시의 C++ 버전을 14버전을 default로 정의함 ----
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

// ---- CMake build 시의 빌드 옵션을 넣음. 예를들면 -O3 같은 optimization 을 넣을 수 있음 ----
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

그다음으로는 제일 중요한 부분 executable을 만드는 구간이다.

// include 폴더를 build 시에 포함하여 빌드한다. 
// 해당 명령어가 없으면 include 폴더를 포함하지 않는다.
include_directories(include 
)

// dependency가 있는 package를 찾는다.
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

// main_exe라는 executable을 만든다. 
// 해당 내용을 가지고 있는 코드는 src/main.cpp뿐이다.
// 코드가 여러개라면 뒷부분에 추가해 주면 된다.
add_executable(main_exe src/main.cpp)

// main_exe라는 executable에 dependency가 있는 package를 적어준다.
// 해당 package는 find_package로 찾은 package이다.
ament_target_dependencies(main_exe rclcpp)

// 사용하는 외부 라이브러리나 내부에서 만들어진 라이브러리를 link 해준다.
target_link_libraries(main_exe )

// 해당 executable을 destination에 빌드하여 넣는다.
// 아래 코드에서는 TARGETS 뒤에있는 main_exe executable을 
// lib/${PROEJCT_NAME} 즉 lib/rock 안에 빌드를 한 결과를 넣겠다는 것이고
// colcon build를 한 위치의 install 내부를 잘 찾아보면 확인 할 수 있다.

install(TARGETS 
  main_exe
  DESTINATION lib/${PROJECT_NAME}
)

 


빌드는 해당 코드들이 포함된 폴더에서 

colcon build

colcon build로 빌드를 하거나

colcon build --packages-select ${PROJECT_NAME}

// e.g.) colcon build --packages-select rock

packages-select로 해당 패키지만 찾아서 빌드 한다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함