[FastDDS 编译] windows + vs2022 纯源码编译调试 fastdds (全网独一种方式)
FastDDS依赖于FastCDR / Asio / tinyxml2 / foonathan_memory / openssl 这几个库, 所以在移植FastDDS前需要先将这几个库移植编译好。
一、windows visual studio 2022
FastCDR库:
1、添加路径

2、工程中添加如下源码

3、在\include\fastcdr目录下增加config.h文件

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _FASTCDR_CONFIG_H_
#define _FASTCDR_CONFIG_H_
#define FASTCDR_VERSION_MAJOR 2
#define FASTCDR_VERSION_MINOR 3
#define FASTCDR_VERSION_MICRO 0
#define FASTCDR_VERSION_STR "2.3.0"
#define FASTCDR_SOURCE
// C++11 support defines
#ifndef HAVE_CXX11
#define HAVE_CXX11 1
#endif // ifndef HAVE_CXX11
// Endianness defines
#ifndef FASTCDR_IS_BIG_ENDIAN_TARGET
#define FASTCDR_IS_BIG_ENDIAN_TARGET 0
#endif // ifndef FASTCDR_IS_BIG_ENDIAN_TARGET
#ifndef FASTCDR_HAVE_FLOAT128
#define FASTCDR_HAVE_FLOAT128 0
#endif // ifndef FASTCDR_HAVE_FLOAT128
#ifndef FASTCDR_SIZEOF_LONG_DOUBLE
#define FASTCDR_SIZEOF_LONG_DOUBLE 8
#endif // ifndef FASTCDR_SIZEOF_LONG_DOUBLE
#if defined(__ARM_ARCH) && __ARM_ARCH <= 7
#define FASTCDR_ARM32
#endif // if defined(__ARM_ARCH) && __ARM_ARCH <= 7
#if defined(__GNUC__) && !defined(__clang__)
#define TEMPLATE_SPEC
#else
#define TEMPLATE_SPEC template<>
#endif // if defined(__GNUC__) && !defined(__clang__)
#endif // _FASTCDR_CONFIG_H_
Asio库:
它是一个header-only库,移植比较简单。
1、添加路径

2、将\asio\include\asio.hpp添加到工程中

Ps:其实也可以不添加,只是为了方便查看。
tinyxml2库:
1、添加路径

2、将tinyxml2.cpp添加到工程中

foonathan_memory库:
1、添加路径

2、工程中添加如下源码

3、在\include\foonathan\memory目录下增加config_impl.hpp和container_node_sizes_impl.hpp
在\include\foonathan\memory\detail目录下增加container_node_sizes_impl.hpp


Ps: 这两个文件需要自己添加,如果使用cmake编译,cmake会自动生成这两个文件。config_impl.hpp文件的源码如下:
// Copyright (C) 2015-2025 Jonathan Müller and foonathan/memory contributors
// SPDX-License-Identifier: Zlib
#ifndef FOONATHAN_MEMORY_IMPL_IN_CONFIG_HPP
#error "do not include this file directly, use config.hpp"
#endif
#include <cstddef>
// This file is normally generated by CMake from src/config.hpp.in.
// For MSBuild/manual builds, we provide sensible defaults here.
// Enable allocation size checks (affects thrown diagnostics only).
#ifndef FOONATHAN_MEMORY_CHECK_ALLOCATION_SIZE
#define FOONATHAN_MEMORY_CHECK_ALLOCATION_SIZE 1
#endif
// Select the default raw allocator type used to build BlockAllocators.
// Must be one of: heap_allocator, new_allocator, malloc_allocator,
// static_allocator, virtual_memory_allocator
#ifndef FOONATHAN_MEMORY_IMPL_DEFAULT_ALLOCATOR
#define FOONATHAN_MEMORY_IMPL_DEFAULT_ALLOCATOR heap_allocator
#endif
// Debug options (tune as needed for your build configuration)
#ifndef FOONATHAN_MEMORY_DEBUG_ASSERT
#define FOONATHAN_MEMORY_DEBUG_ASSERT 0
#endif
#ifndef FOONATHAN_MEMORY_DEBUG_FILL
#define FOONATHAN_MEMORY_DEBUG_FILL 0
#endif
#ifndef FOONATHAN_MEMORY_DEBUG_FENCE
#define FOONATHAN_MEMORY_DEBUG_FENCE 0
#endif
#ifndef FOONATHAN_MEMORY_DEBUG_LEAK_CHECK
#define FOONATHAN_MEMORY_DEBUG_LEAK_CHECK 0
#endif
#ifndef FOONATHAN_MEMORY_DEBUG_POINTER_CHECK
#define FOONATHAN_MEMORY_DEBUG_POINTER_CHECK 0
#endif
#ifndef FOONATHAN_MEMORY_DEBUG_DOUBLE_DEALLOC_CHECK
#define FOONATHAN_MEMORY_DEBUG_DOUBLE_DEALLOC_CHECK 0
#endif
// Whether the library provides explicit template instantiations in the .cpps.
// When building as sources in your project, set to 0 to avoid ODR/link issues.
#ifndef FOONATHAN_MEMORY_EXTERN_TEMPLATE
#define FOONATHAN_MEMORY_EXTERN_TEMPLATE 0
#endif
// Temporary stack mode (2 = full feature; see docs for details)
#ifndef FOONATHAN_MEMORY_TEMPORARY_STACK_MODE
#define FOONATHAN_MEMORY_TEMPORARY_STACK_MODE 2
#endif
container_node_sizes_impl.hpp文件的源码如下:(从ros2-jazzy-20250820-windows-release-amd64Z中提取的)
// The following section was autogenerated by get_container_node_sizes.cmake
//=== BEGIN AUTOGENERATED SECTION ===//
namespace detail
{
template <std::size_t Alignment>
struct forward_list_node_size;
template <>
struct forward_list_node_size<1>
: std::integral_constant<std::size_t, 15>
{};
template <>
struct forward_list_node_size<2>
: std::integral_constant<std::size_t, 14>
{};
template <>
struct forward_list_node_size<4>
: std::integral_constant<std::size_t, 12>
{};
template <>
struct forward_list_node_size<8>
: std::integral_constant<std::size_t, 8>
{};
} // namespace detail
template <typename T>
struct forward_list_node_size
: std::integral_constant<std::size_t,
detail::forward_list_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct list_node_size;
template <>
struct list_node_size<1>
: std::integral_constant<std::size_t, 23>
{};
template <>
struct list_node_size<2>
: std::integral_constant<std::size_t, 22>
{};
template <>
struct list_node_size<4>
: std::integral_constant<std::size_t, 20>
{};
template <>
struct list_node_size<8>
: std::integral_constant<std::size_t, 16>
{};
} // namespace detail
template <typename T>
struct list_node_size
: std::integral_constant<std::size_t,
detail::list_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct set_node_size;
template <>
struct set_node_size<1>
: std::integral_constant<std::size_t, 31>
{};
template <>
struct set_node_size<2>
: std::integral_constant<std::size_t, 30>
{};
template <>
struct set_node_size<4>
: std::integral_constant<std::size_t, 28>
{};
template <>
struct set_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct set_node_size
: std::integral_constant<std::size_t,
detail::set_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct multiset_node_size;
template <>
struct multiset_node_size<1>
: std::integral_constant<std::size_t, 31>
{};
template <>
struct multiset_node_size<2>
: std::integral_constant<std::size_t, 30>
{};
template <>
struct multiset_node_size<4>
: std::integral_constant<std::size_t, 28>
{};
template <>
struct multiset_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct multiset_node_size
: std::integral_constant<std::size_t,
detail::multiset_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct unordered_set_node_size;
template <>
struct unordered_set_node_size<1>
: std::integral_constant<std::size_t, 23>
{};
template <>
struct unordered_set_node_size<2>
: std::integral_constant<std::size_t, 22>
{};
template <>
struct unordered_set_node_size<4>
: std::integral_constant<std::size_t, 20>
{};
template <>
struct unordered_set_node_size<8>
: std::integral_constant<std::size_t, 16>
{};
} // namespace detail
template <typename T>
struct unordered_set_node_size
: std::integral_constant<std::size_t,
detail::unordered_set_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct unordered_multiset_node_size;
template <>
struct unordered_multiset_node_size<1>
: std::integral_constant<std::size_t, 23>
{};
template <>
struct unordered_multiset_node_size<2>
: std::integral_constant<std::size_t, 22>
{};
template <>
struct unordered_multiset_node_size<4>
: std::integral_constant<std::size_t, 20>
{};
template <>
struct unordered_multiset_node_size<8>
: std::integral_constant<std::size_t, 16>
{};
} // namespace detail
template <typename T>
struct unordered_multiset_node_size
: std::integral_constant<std::size_t,
detail::unordered_multiset_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct map_node_size;
template <>
struct map_node_size<1>
: std::integral_constant<std::size_t, 30>
{};
template <>
struct map_node_size<2>
: std::integral_constant<std::size_t, 28>
{};
template <>
struct map_node_size<4>
: std::integral_constant<std::size_t, 32>
{};
template <>
struct map_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct map_node_size
: std::integral_constant<std::size_t,
detail::map_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct multimap_node_size;
template <>
struct multimap_node_size<1>
: std::integral_constant<std::size_t, 30>
{};
template <>
struct multimap_node_size<2>
: std::integral_constant<std::size_t, 28>
{};
template <>
struct multimap_node_size<4>
: std::integral_constant<std::size_t, 32>
{};
template <>
struct multimap_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct multimap_node_size
: std::integral_constant<std::size_t,
detail::multimap_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct unordered_map_node_size;
template <>
struct unordered_map_node_size<1>
: std::integral_constant<std::size_t, 22>
{};
template <>
struct unordered_map_node_size<2>
: std::integral_constant<std::size_t, 20>
{};
template <>
struct unordered_map_node_size<4>
: std::integral_constant<std::size_t, 16>
{};
template <>
struct unordered_map_node_size<8>
: std::integral_constant<std::size_t, 16>
{};
} // namespace detail
template <typename T>
struct unordered_map_node_size
: std::integral_constant<std::size_t,
detail::unordered_map_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct unordered_multimap_node_size;
template <>
struct unordered_multimap_node_size<1>
: std::integral_constant<std::size_t, 22>
{};
template <>
struct unordered_multimap_node_size<2>
: std::integral_constant<std::size_t, 20>
{};
template <>
struct unordered_multimap_node_size<4>
: std::integral_constant<std::size_t, 16>
{};
template <>
struct unordered_multimap_node_size<8>
: std::integral_constant<std::size_t, 16>
{};
} // namespace detail
template <typename T>
struct unordered_multimap_node_size
: std::integral_constant<std::size_t,
detail::unordered_multimap_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct shared_ptr_stateless_node_size;
template <>
struct shared_ptr_stateless_node_size<1>
: std::integral_constant<std::size_t, 24>
{};
template <>
struct shared_ptr_stateless_node_size<2>
: std::integral_constant<std::size_t, 24>
{};
template <>
struct shared_ptr_stateless_node_size<4>
: std::integral_constant<std::size_t, 24>
{};
template <>
struct shared_ptr_stateless_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct shared_ptr_stateless_node_size
: std::integral_constant<std::size_t,
detail::shared_ptr_stateless_node_size<alignof(T)>::value + sizeof(T)>
{};
namespace detail
{
template <std::size_t Alignment>
struct shared_ptr_stateful_node_size;
template <>
struct shared_ptr_stateful_node_size<1>
: std::integral_constant<std::size_t, 32>
{};
template <>
struct shared_ptr_stateful_node_size<2>
: std::integral_constant<std::size_t, 32>
{};
template <>
struct shared_ptr_stateful_node_size<4>
: std::integral_constant<std::size_t, 32>
{};
template <>
struct shared_ptr_stateful_node_size<8>
: std::integral_constant<std::size_t, 32>
{};
} // namespace detail
template <typename T>
struct shared_ptr_stateful_node_size
: std::integral_constant<std::size_t,
detail::shared_ptr_stateful_node_size<alignof(T)>::value + sizeof(T)>
{};
//=== END AUTOGENERATED SECTION ===//
Openssl库:
Openssl使用静态库, 编译过程可以参考Windows上源码编译生成OpenSSL静态库和动态库_openssl windows 编译-CSDN博客 (https://blog.csdn.net/qq_24628877/article/details/146636048)
这边直接从这篇文章中下载编译好的资源了。
1、先把下载好的静态放到工程目录中

2、添加静态库的路径

3、添加如下静态库

Ps: crypt32.lib是windows的系统静态库,openssl用它了,所以要添加
4、添加头文件路径

FastDDS库:
1、添加头文件路径

2、工程中添加源文件
Ps: 别漏了.cxx文件











3、修改如下红框中的文件的编译输出.obj文件名字:
该文件右键属性->c/c++->Output Files->Object File Name修改为 $(IntDir)%(Filename)2.obj
或$(IntDir)%(Filename)3.obj。
因为工程中有其他同名文件,如果不修改,后编译的同名文件输出.obj会覆盖先编译的,从而导致缺失先编译的符号链接。
如下文件修改为$(IntDir)%(Filename)2.obj



如下文件修改为$(IntDir)%(Filename)3.obj

4、编译语言要选c++17,选c++20会出现如下报错
'destroy': is not a member of 'std::allocator<_Ty>',这是因为C++17 以后直接调用 allocator.construct / destroy 在自定义或标准 allocator 上可能不可用(C++20 标准鼓励通过 std::allocator_traits)
5、工程属性->c/c++->command line中添加
/D "_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS" /D "WIN32_LEAN_AND_MEAN" /D "NOMINMAX" /D "BOOST_USE_WINDOWS_H" /D "_HAS_DEPRECATED_ALLOCATOR_MEMBERS=1" -U UNICODE /FS

6、添加静态库Shlwapi.lib
因为里面调用的函数PathMatchSpecA属于这个库

7、在\include\fastdds目录下增加config.hpp文件,该文件源码如下:

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FASTDDS__CONFIG_H
#define FASTDDS__CONFIG_H
#define FASTDDS_VERSION_MAJOR 3
#define FASTDDS_VERSION_MINOR 3
#define FASTDDS_VERSION_MICRO 0
#define FASTDDS_VERSION_STR "3.3.0"
#define FASTDDS_GEN_API_VER 3
// C++20 support defines
#ifndef HAVE_CXX20
#define HAVE_CXX20 1
#endif /* ifndef HAVE_CXX20 */
// C++17 support defines
#ifndef HAVE_CXX17
#define HAVE_CXX17 1
#endif /* ifndef HAVE_CXX17 */
// C++14 support defines
#ifndef HAVE_CXX14
#define HAVE_CXX14 1
#endif /* ifndef HAVE_CXX14 */
// C++1Y support defines
#ifndef HAVE_CXX1Y
#define HAVE_CXX1Y 1
#endif /* ifndef HAVE_CXX1Y */
// C++11 support defines
#ifndef HAVE_CXX11
#define HAVE_CXX11 1
#endif /* ifndef HAVE_CXX11 */
// Endianness defines
#ifndef FASTDDS_IS_BIG_ENDIAN_TARGET
#define FASTDDS_IS_BIG_ENDIAN_TARGET 0
#endif /* ifndef FASTDDS_IS_BIG_ENDIAN_TARGET */
// Security
#ifndef HAVE_SECURITY
#define HAVE_SECURITY 1
#endif /* ifndef HAVE_SECURITY */
//Sqlite3 support
#ifndef HAVE_SQLITE3
#define HAVE_SQLITE3 0
#endif /* ifndef HAVE_SQLITE3 */
// Using thirdparty shared_mutex
#define USE_THIRDPARTY_SHARED_MUTEX 0
// TLS support
#ifndef TLS_FOUND
#define TLS_FOUND 1
#endif /* ifndef TLS_FOUND */
// Strict real-time
#ifndef HAVE_STRICT_REALTIME
#define HAVE_STRICT_REALTIME 0
#endif /* ifndef HAVE_STRICT_REALTIME */
/* Log Macros */
// Enable compilation for eProsima Log Macros
#ifndef ENABLE_OLD_LOG_MACROS_
#define ENABLE_OLD_LOG_MACROS_ 1
#endif /* ifndef ENABLE_OLD_LOG_MACROS_ */
// Log Info
#ifndef FASTDDS_ENFORCE_LOG_INFO
#define FASTDDS_ENFORCE_LOG_INFO
#endif // ifndef FASTDDS_ENFORCE_LOG_INFO
#ifndef HAVE_LOG_NO_INFO
#define HAVE_LOG_NO_INFO 0
#endif /* ifndef HAVE_LOG_NO_INFO */
// Log Warning
#ifndef HAVE_LOG_NO_WARNING
#define HAVE_LOG_NO_WARNING 0
#endif /* ifndef HAVE_LOG_NO_WARNING */
// Log Error
#ifndef HAVE_LOG_NO_ERROR
#define HAVE_LOG_NO_ERROR 0
#endif /* ifndef HAVE_LOG_NO_ERROR */
// Statistics
#define FASTDDS_STATISTICS
#define FASTDDS_SOURCE
// Deprecated macro
#if __cplusplus >= 201402L
#define FASTDDS_DEPRECATED(msg) [[ deprecated(msg) ]]
#elif defined(__GNUC__) || defined(__clang__)
#define FASTDDS_DEPRECATED(msg) __attribute__ ((deprecated(msg)))
#elif defined(_MSC_VER)
#define FASTDDS_DEPRECATED(msg) __declspec(deprecated(msg))
#else
#define FASTDDS_DEPRECATED(msg)
#endif /* if __cplusplus >= 201402L */
// Deprecation with version
#define FASTDDS_DEPRECATED_UNTIL(major, entity_name, msg) \
static_assert(FASTDDS_VERSION_MAJOR < major, #entity_name " reached deprecation version " #major); \
FASTDDS_DEPRECATED(#entity_name " has been deprecated and will be removed on major version " #major ". " msg)
#define FASTDDS_TODO_BEFORE(major, minor, msg) \
static_assert((FASTDDS_VERSION_MAJOR < major) || \
(FASTDDS_VERSION_MAJOR == major && FASTDDS_VERSION_MINOR < minor), \
"TODO before version " #major "." #minor " : " #msg)
#endif // FASTDDS__CONFIG_H
最后,看下整个工程的目录吧

更多推荐
所有评论(0)