Skip to content

How to build your own application

Ákos Milánkovich edited this page Jul 26, 2019 · 2 revisions

To build your own application called simulator with CMake, use the following template, which requires, that the dependencies of Communication Library (protobuf and czmq) and the Communication Library are built and can be linked. CmakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(simulator)

set(LIBSWARMIO_BUILD_PATH "/home/user/Documents/swarmio")

set(SOURCE_FILES
  "src/main.cpp"
  "src/ExampleDevice.cpp"
  "src/LinearPathTelemetrySimulator.cpp"
)
include_directories(simulator  ${PROJECT_SOURCE_DIR}/include)
include_directories(simulator  ${LIBSWARMIO_BUILD_PATH}/libswarmio/include)
include_directories(simulator PUBLIC ${LIBSWARMIO_BUILD_PATH}/build/main-prefix/include)

link_directories(simulator -L${LIBSWARMIO_BUILD_PATH}/build/main-prefix/lib)

add_executable(simulator ${SOURCE_FILES})

# Add export/import macros
if (MSVC)
	target_compile_definitions(simulator 
		PRIVATE "SWARMIO_API=__declspec(dllexport)"
		INTERFACE "SWARMIO_API=__declspec(dllimport)"
		PUBLIC "PROTOBUF_USE_DLLS"
	)
else()
	target_compile_definitions(simulator 
		PRIVATE "SWARMIO_API=__attribute__((visibility(\"default\")))"
		INTERFACE "SWARMIO_API="
		PUBLIC "PROTOBUF_USE_DLLS"
	)
endif()

# Create directory for the files generated by protobuf
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")

# Generate C++ files for Protobuf definitions
get_target_property(_sources simulator SOURCES)
foreach(_source ${_sources})

	# Match .proto files
	if(_source MATCHES "^proto/(.+)\.proto$")

		# Build output file names
		set(_outputs
			"${CMAKE_CURRENT_BINARY_DIR}/include/${CMAKE_MATCH_1}.pb.h"
			"${CMAKE_CURRENT_BINARY_DIR}/include/${CMAKE_MATCH_1}.pb.cc"
		)

		# Generate sources
		add_custom_command(
			OUTPUT ${_outputs}
			COMMAND ${TOOL_PROTOC}
			ARGS 
				--cpp_out dllexport_decl=SWARMIO_API:${CMAKE_CURRENT_BINARY_DIR}/include 
				--proto_path ${CMAKE_CURRENT_SOURCE_DIR}/proto 
				${CMAKE_CURRENT_SOURCE_DIR}/${_source}
			DEPENDS
				${_source} 
			COMMENT "Running CPP protocol buffer compiler on ${_source}"
			VERBATIM
		)

		# Add sources to target
		target_sources(simulator PRIVATE ${_outputs})

	endif()

endforeach()

# Ignore protobuf related warnings
if (MSVC)
	target_compile_options(simulator PUBLIC
		"/wd4251"
		"/wd4996"
		"/wd4275"
		"/wd4661"
	)
endif()

# Link libraries
target_link_libraries(simulator PRIVATE
  pthread
  ${LIBSWARMIO_BUILD_PATH}/build/main-prefix/lib/libswarmio.so
  ${LIBSWARMIO_BUILD_PATH}/build/main-prefix/lib/libprotobufd.so
  ${LIBSWARMIO_BUILD_PATH}/build/main-prefix/lib/libczmq.so
  ${LIBSWARMIO_BUILD_PATH}/build/main-prefix/lib/libg3logger.so
)
Clone this wiki locally