Skip to content

Commit

Permalink
Set up CMake package and install
Browse files Browse the repository at this point in the history
  • Loading branch information
dpkoch committed Mar 8, 2018
1 parent cbef6dc commit 79d88e9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
25 changes: 21 additions & 4 deletions CMakeLists.txt
Expand Up @@ -21,16 +21,33 @@ add_library(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

# examples
option(BUILD_EXAMPLES "Build examples" OFF)
option(ASYNC_COMM_BUILD_EXAMPLES "Build examples" OFF)

if(BUILD_EXAMPLES)
if(ASYNC_COMM_BUILD_EXAMPLES)
add_executable(udp_hello_world examples/udp_hello_world.cpp)
target_link_libraries(udp_hello_world ${PROJECT_NAME})

add_executable(serial_loopback examples/serial_loopback.cpp)
target_link_libraries(serial_loopback ${PROJECT_NAME})
endif(BUILD_EXAMPLES)
endif(ASYNC_COMM_BUILD_EXAMPLES)

# ROS release
# install
set(LIB_DEST lib/${PROJECT_NAME})
set(INCLUDE_DEST include)

install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${LIB_DEST}
)
install(DIRECTORY include/${PROJECT_NAME}
DESTINATION ${INCLUDE_DEST}
FILES_MATCHING PATTERN "*.h"
)

# install CMake package configuration
install(EXPORT ${PROJECT_NAME}-targets DESTINATION ${LIB_DEST})
install(FILES ${PROJECT_NAME}-config.cmake DESTINATION ${LIB_DEST})

# install package.xml for ROS release
install(FILES package.xml DESTINATION share/${PROJECT_NAME})

50 changes: 42 additions & 8 deletions README.md
Expand Up @@ -7,25 +7,59 @@ It uses the [Boost.Asio](http://www.boost.org/doc/libs/master/doc/html/boost_asi

## Including in your project

The following instructions are for a project using Git for version control and CMake for a build system, but should serve as a starting point for other setups.
There are two ways to use the `async_comm` library in your project:

The easiest way to embed the `async_comm` library in your project is as a [Git submodule](https://git-scm.com/docs/gitsubmodules). For example, to put `async_comm` in the `lib/async_comm directory`, run the following from the root of your project:
1. Build and install the library on your system, then use CMake's `find_package()` functionality
2. Include the async_comm as a submodule in your project

### System install

First, download and install the library:

```bash
git submodule add https://github.com/dpkoch/async_comm.git lib/async_comm
git clone https://github.com/dpkoch/async_comm.git
cd async_comm
mkdir build && cd build/
cmake .. && make
sudo make install
```

Then add the following to your `CMakeLists.txt` to build the `async_comm` library and make the headers accessible in your project:
Then, in your project do something like this in your CMakeLists.txt:

```CMake
cmake_minimum_required(VERSION 2.8.11)
project(my_project)
set(CMAKE_CXX_FLAGS "-std=c++11")
add_subdirectory(lib/async_comm)
include_directories(lib/async_comm/include)
find_package(async_comm REQUIRED)
include_directories(${async_comm_INCLUDE_DIRS})
add_executable(my_project src/my_project.cpp)
target_link_libraries(testing ${async_comm_LIBRARIES})
```

Then link your executable to the `async_comm` library with something like
### Including as a submodule

If you don't want to go with the system install option, the next easiest way to embed the `async_comm` library in your project is as a [Git submodule](https://git-scm.com/docs/gitsubmodules). The following instructions are for a project using Git for version control and CMake for a build system, but should serve as a starting point for other setups.

For example, to put `async_comm` in the `lib/async_comm directory`, run the following from the root of your project:

```bash
git submodule add https://github.com/dpkoch/async_comm.git lib/async_comm
```

Your CMakeLists.txt file would then look something like this:

```CMake
cmake_minimum_required(VERSION 2.8.11)
project(my_project)
set(CMAKE_CXX_FLAGS "-std=c++11")
add_subdirectory(lib/async_comm)
include_directories(lib/async_comm/include)
add_executable(my_program src/my_program.cpp)
target_link_libraries(my_program async_comm)
```
Expand Down Expand Up @@ -60,7 +94,7 @@ where `serial_` is an instance of `async_comm::Serial`.

## Examples

There are two simple examples provided in the repository. To build the examples, run CMake with the `-DBUILD_EXAMPLES=ON` flag.
There are two simple examples provided in the repository. To build the examples, run CMake with the `-DASYNC_COMM_BUILD_EXAMPLES=ON` flag.

- `examples/serial_loopback.cpp`: Designed for use with a USB-to-UART adapter with the RX and TX pins connected together (loopback). Sends a series of bytes out and prints them to the console as they are received back.
- `examples/udp_hello_world.cpp`: Opens two UDP objects listening on different ports on the local host, and then uses each to send a simple "hello world" message to the other.
4 changes: 4 additions & 0 deletions async_comm-config.cmake
@@ -0,0 +1,4 @@
get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(${SELF_DIR}/async_comm-targets.cmake)
get_filename_component(aysnc_comm_INCLUDE_DIRS "${SELF_DIR}/../../include/async_comm" ABSOLUTE)
set(async_comm_LIBRARIES async_comm)

0 comments on commit 79d88e9

Please sign in to comment.