我正在尝试实现一个struct person,我需要隐藏一些字段或使其保持不变。 创建私有字段的技巧。
标头:
#pragma once
#define NAME_MAX_LEN 20
typedef struct _person {
float wage;
int groupid;
} Person;
const char const *getName (Person *p);
int getId (Person *p);
/// OTHER FUNCTIONS
Run Code Online (Sandbox Code Playgroud)
资源
#include "person.h"
struct _person
{
int id;
float wage;
int groupid;
char name[NAME_MAX_LEN];
};
/// FUNCTIONS
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会说 person.c:7:8: error: redefinition a 'struct _person' struct _person
我可以将其写在标题中,但是在此之后,就不能使用结构的字段。
typedef struct _person Person;
Run Code Online (Sandbox Code Playgroud) 如何在 cmake 中创建一个将所有 C++ 文件收集到一个标题中的项目?
我有这个项目结构。
/
project/
folder1/
file.cpp
file.hpp
folder2/
...etc
CMakeLists.txt
tests/
test.cpp
CMakeLists.txt
CMakeList.txt
Run Code Online (Sandbox Code Playgroud)
根 cmakelists.txt
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject"
LANGUAGES C CXX)
set(CMAKE_EXECUTABLE_SUFFIX ".exe")
include(GNUInstallDirs)
add_subdirectory ("project")
option(ENABLE_TESTING OFF)
if (ENABLE_TESTING)
enable_testing()
add_subdirectory("tests")
endif()
Run Code Online (Sandbox Code Playgroud)
项目中的 CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
file(GLOB projectSRC
"*/*.cpp"
"*/*.hpp"
"*.cpp"
"*.hpp"
)
add_library(project INTERFACE)
message(STATUS "CMake inatall directory: " ${CMAKE_INSTALL_INCLUDEDIR})
target_include_directories(project
INTERFACE
$<BUILD_INTERFACE:${PROJECT_INCLUDE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
Run Code Online (Sandbox Code Playgroud)
并测试 cmakelist.txt
cmake_minimum_required (VERSION 3.8)
# install Catch2 testing library
# (https://github.com/catchorg/Catch2/blob/master/docs/cmake-integration.md#installing-catch2-from-git-repository or use packet manager) …Run Code Online (Sandbox Code Playgroud)