<<TOC(4)>>

=== 패키지 빌드하기 ===
모든 시스템 의존성이 설치되었다면 이제 새로운 패키지를 만들어 볼 차례입니다.

{{{#!wiki blue/solid
'''Note:''' `apt`또는 다른 종류의 패키지 관리자를 통해 ROS를 설치하셨으면, 모든 시스템 의존성이 설치되어 있을 것입니다.
}}}

본격적으로 시작하기 전에 환경설정 파일을 등록하는 것을 기억하시길 바랍니다. 우분투 환경에서는 아래의 예시를 참고하시면 됩니다.

{{{
$ source /opt/ros/groovy/setup.bash
}}}

==== catkin_make의 사용 ====

[[catkin/commands/catkin_make|catkin_make(en)]]는 catkin을 이용한 일련의 작업 과정에서 쓰이는 편리한 도구입니다. 이는 일반적으로 CMake 작업환경에서의 `cmake`와 `make`가 합쳐져 있는 도구라 생각하시면 됩니다.

사용 예시:


{{{
# In a catkin workspace
$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]
}}}

CMake 작업 과정에 익숙하지 않으신 분들을 위해 부분적으로 나누어 설명을 하겠습니다.

{{{#!wiki blue/solid
'''Note:''' 아래의 명령어는 CMake가 일반적으로 어떻게 동작하는지 보여주기 위한 예제로, 실제로 동작하지 않을 수 있습니다. 
}}}

{{{
# In a CMake project
$ mkdir build
$ cd build
$ cmake ..
$ make
$ make install  # (optionally)
}}}

이 과정은 각각의 CMake프로젝트에 대해 수행됩니다. 같은 작업공간 안의 catkin프로젝트는 동시에 빌드할 수 있습니다. 이 방법은 아래의 예시를 보시기 바랍니다.

{{{
# catkin 작업공간 경로에서
$ catkin_make
$ catkin_make install  # (optionally)
}}}

위의 명령어들은 catkin 프로젝트의 `src`폴더를 찾아 빌드합니다. 
The above commands will build any catkin projects found in the `src` folder. 이는 recommendations set([[http://ros.org/reps/rep-0128.html|REP128]])규칙을 따릅니다. 패키지의 소스코드가 다른 폴더 `my_src`에 들어있다면 catkin_make 명령은 아래와 같이 사용합니다.

{{{#!wiki blue/solid
'''Note:''' 이 명령은 `my_src`가 없으면 동작하지 않습니다.
}}}

{{{
# catkin 작업공간에서
$ catkin_make --source my_src
$ catkin_make install --source my_src  # (optionally)
}}}

보다 자세한 사용 방법은 이 링크를 참고하세요.
[[catkin/commands/catkin_make]]

==== 패키지 빌드하기 ====
{{{#!wiki blue/solid
직접 작성한 코드를 빌드하려는 분들은 `CMakeLists.txt`를 수정해야 할 수도 있습니다. 뒷 부분의 자습서(아래의 링크)를 참고하세요. [[ko/ROS/Tutorials/WritingPublisherSubscriber(c++)|(C++)]]/[[ko/ROS/Tutorials/WritingPublisherSubscriber(python)|(Python)]]
}}}
이전의 자습서에서 [[ko/catkin/Tutorials/create_a_workspace|catkin workspace]]와 `beginner_tutorials` 패키지를 만들었습니다.[[ko/ROS/Tutorials/CreatingPackage|패키지 만들기]]. catkin 작업공간으로 이동해서 `src`폴더의 내용을 확인합니다.

{{{
$ cd ~/catkin_ws/
$ ls src
}}}
 {{{
beginner_tutorials/  CMakeLists.txt@  
}}}

전의 자습서에서 `beginner_tutorials`아래에 [[catkin/commands/catkin_create_pkg|catkin_create_pkg]]가 생성한 파일들을 볼 수 있을 것입니다. 우리는 이제 [[catkin/commands/catkin_make|catkin_make]]를 사용해 이 패키지를 빌드할 것입니다.

{{{
$ catkin_make
}}}

`cmake`에서 많은 결과물을 생성하는 것을 볼 수 있습니다. 그리고 `make`를 합니다. 

 {{{
Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build
Devel space: /home/user/catkin_ws/devel
Install space: /home/user/catkin_ws/install
####
#### Running command: "cmake /home/user/catkin_ws/src
-DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel
-DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"
####
-- The C compiler identification is GNU 4.2.1
-- The CXX compiler identification is Clang 4.0.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/groovy
-- This workspace overlays: /opt/ros/groovy
-- Found PythonInterp: /usr/bin/python (found version "2.7.1") 
-- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
-- Found gtest: gtests will be built
-- catkin 0.5.51
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing packages in topological order:
-- ~~  - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ add_subdirectory(beginner_tutorials)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/catkin_ws/build
####
#### Running command: "make -j4" in "/home/user/catkin_ws/build"
####
}}}

[[catkin/commands/catkin_make|catkin_make]]는 먼저 각각의 `space`들의 경로를 표시합니다. space에 대한 설명은 [[http://ros.org/reps/rep-0128.html|REP128]]에 서술되어 있고, [[catkin/workspaces]]위키에 문서화 되어 있습니다. 이 기본값들이 catkin작업공간 안에 새로운 폴더를 만든 것을 눈여겨 보시기 바랍니다. `ls`명령을 통해 살펴봅니다.

{{{
$ ls
}}}
 {{{
build
devel
src
}}}

`build`폴더는 [[catkin/workspaces#Build_Space|build space]]의 기본 경로입니다. 이 곳은 패키지의 빌드작업을 위해 `cmake` 와 `make`가 호출될 때 사용되는 곳입니다.`devel`폴더는 패키지를 시스템에 설치하기 전, 개발과정에서 쓰이는 실행파일과 라이브러리들이 저장되는 [[catkin/workspaces#Development_.28Devel.29_Space|devel space]]의 기본 경로입니다.