提交 83e88b9d 编写于 作者: 小代码2016's avatar 小代码2016

基本的开发环境

上级 fe653216
/build
/bin
/lib
\ No newline at end of file
{
"files.associations": {
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}
}
\ No newline at end of file
# 该项目所需 cmake 的最小版本, 如果 cmake 版本小于设置的版本, cmake 将停止处理并报错
cmake_minimum_required(VERSION 3.25)
project(khl_xml CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(khl_common_set_project_debug)
# 编译模式是否为 debug
cmake_language(CALL khl_common_set_project_debug)
message(STATUS "====> project_debug : ${project_debug}")
message(STATUS "====> CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
# 设置 debug 库后缀
if(${project_debug})
set(CMAKE_DEBUG_POSTFIX d)
endif()
# 消除警告 warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
# 参考: https://blog.csdn.net/five_east_west/article/details/125382251
if(${CMAKE_HOST_WIN32})
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif()
set(khl_math_lib_name khl_math${CMAKE_DEBUG_POSTFIX})
# 添加预处理器定义, 主要用于 windows
if(${project_debug})
add_compile_definitions(_DEBUG)
endif()
set(khl_xml_lib_name khl_xml)
set(khl_xml_main_name khl_xml_main)
set(tinyxml2_lib_name tinyxml2)
set(tinyxml2_lib_full_name ${CMAKE_STATIC_LIBRARY_PREFIX}${tinyxml2_lib_name}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX})
set(tinyxml2_include_dir ${CMAKE_SOURCE_DIR}/${tinyxml2_lib_name})
# windows 平台
# 把 .dll 和 .lib 复制到 lib 目录
function(copy_dll_to_lib dll_name)
if(${CMAKE_HOST_WIN32})
set(dll_full_name ${CMAKE_SHARED_LIBRARY_PREFIX}${dll_name}${CMAKE_DEBUG_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX})
set(dll_lib_name ${CMAKE_SHARED_LIBRARY_PREFIX}${dll_name}${CMAKE_DEBUG_POSTFIX}.lib)
add_custom_command(TARGET ${dll_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/build/src/${dll_name}/${CMAKE_BUILD_TYPE}/${dll_full_name} ${CMAKE_SOURCE_DIR}/lib/${dll_full_name})
add_custom_command(TARGET ${dll_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/build/src/${dll_name}/${CMAKE_BUILD_TYPE}/${dll_lib_name} ${CMAKE_SOURCE_DIR}/lib/${dll_lib_name})
endif()
endfunction()
# windows 平台
# 把 .dll 复制到 bin 目录
function(copy_dll_to_exe dll_name)
if(${CMAKE_HOST_WIN32})
set(dll_full_name ${CMAKE_SHARED_LIBRARY_PREFIX}${dll_name}${CMAKE_DEBUG_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX})
add_custom_command(TARGET ${khl_xml_main_name} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/build/src/${dll_name}/${CMAKE_BUILD_TYPE}/${dll_full_name} ${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE})
endif()
endfunction()
add_subdirectory(src)
add_subdirectory(tinyxml2)
# 添加测试项目所在目录
add_subdirectory(test)
# 必须添加这两行, 而且必须在顶层 CMakeLists.txt 添加
enable_testing()
add_test(NAME hello_test COMMAND hello_test)
# -*- coding: utf-8 -*-
import argparse
import os
import platform
import os.path
app_name = 'khl_xml_main'
app_test_name = 'khl_xml_test'
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
KEY_CM_ACTION = 'cm_action'
ARG_PREFIX = '--'
SUB_COMMAND_BUILD = 'build'
SUB_COMMAND_RE_BUILD = 'rebuild'
SUB_COMMAND_RUN = 'run'
SUB_COMMAND_TEST = 'test'
SUB_COMMAND_CLEAN = 'clean'
def register_build_args():
build_parsers = subparsers.add_parser(SUB_COMMAND_BUILD, help='构建')
build_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_BUILD)
def register_re_build_args():
rebuild_parsers = subparsers.add_parser(SUB_COMMAND_RE_BUILD, help='重新构建')
rebuild_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_RE_BUILD)
def register_run_args():
run_parsers = subparsers.add_parser(SUB_COMMAND_RUN, help='运行')
run_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_RUN)
def register_test_args():
test_parsers = subparsers.add_parser(SUB_COMMAND_TEST, help='测试')
test_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_TEST)
def register_clean_args():
test_parsers = subparsers.add_parser(SUB_COMMAND_CLEAN, help='清理')
test_parsers.add_argument(ARG_PREFIX + KEY_CM_ACTION, help='动作类型', default=SUB_COMMAND_CLEAN)
def is_windows():
return 'Windows' == platform.system()
def get_build_dir():
return os.getcwd() + os.path.sep + 'build'
def get_bin_dir():
return os.getcwd() + os.path.sep + 'bin'
def get_lib_dir():
return os.getcwd() + os.path.sep + 'lib'
def execute_build():
if is_windows():
os.system('cmake -S . -B build -DCMAKE_CONFIGURATION_TYPES=Release && cmake --build build --config=release')
else:
os.system('cmake -S . -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build --config=release')
def execute_re_build():
execute_clean()
if is_windows():
os.system('cmake -S . -B build -DCMAKE_CONFIGURATION_TYPES=Release && cmake --build build --config=release')
else:
os.system('cmake -S . -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build --config=release')
def execute_run():
execute_build()
if is_windows():
os.system(".\\bin\\Release\\" + app_name)
else:
os.system('./bin/' + app_name)
def execute_test():
execute_build()
if is_windows():
os.system(".\\bin\\Release\\" + app_test_name)
else:
os.system('./bin/' + app_test_name)
def execute_clean():
if is_windows():
if os.path.exists(get_build_dir()):
os.system('rd /q/s build')
if os.path.exists(get_bin_dir()):
os.system('rd /q/s bin')
if os.path.exists(get_lib_dir()):
os.system('rd /q/s lib')
else:
if os.path.exists(get_build_dir()):
os.system('rm -rf build')
if os.path.exists(get_bin_dir()):
os.system('rm -rf bin')
if os.path.exists(get_lib_dir()):
os.system('rm -rf lib')
if __name__ == '__main__':
register_build_args()
register_re_build_args()
register_run_args()
register_test_args()
register_clean_args()
args = parser.parse_args()
if 0 == len(args.__dict__):
execute_test()
else:
if SUB_COMMAND_BUILD == args.__dict__[KEY_CM_ACTION]:
execute_build()
if SUB_COMMAND_RE_BUILD == args.__dict__[KEY_CM_ACTION]:
execute_re_build()
if SUB_COMMAND_RUN == args.__dict__[KEY_CM_ACTION]:
execute_run()
if SUB_COMMAND_TEST == args.__dict__[KEY_CM_ACTION]:
execute_test()
if SUB_COMMAND_CLEAN == args.__dict__[KEY_CM_ACTION]:
execute_clean()
#[[
编译模式是否为 debug
var:
project_debug [1,0]
CMAKE_BUILD_TYPE [Debug,Release]
useage:
cmake_language(CALL khl_common_set_project_debug)
if(${project_debug})
# dosometing for debug
else()
# dosometing for release
endif()
]]
function(khl_common_set_project_debug)
message(STATUS "====> khl_common_set_project_debug : 判断是否为 debug 模式")
# msvc 的 debug 与 release 不能通过 CMAKE_BUILD_TYPE 判断
# 应该使用 CMAKE_CONFIGURATION_TYPES
if(${CMAKE_HOST_WIN32})
string(REGEX MATCH "^[Dd][Ee][Bb][Uu][Gg]" DEBUG_CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
if(DEBUG_CONFIG_TYPE)
set(project_debug 1 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Debug" PARENT_SCOPE)
endif()
string(REGEX MATCH "^[Rr][Ee][Ll][Ee][Aa][Ss][Ee]" RELEASE_CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
if(RELEASE_CONFIG_TYPE)
set(project_debug 0 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Release" PARENT_SCOPE)
endif()
# unix 判断编译模式
elseif(${CMAKE_HOST_UNIX})
if(CMAKE_BUILD_TYPE)
string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
if(${BUILD_TYPE} STREQUAL "debug")
set(project_debug 1 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Debug" PARENT_SCOPE)
elseif(${BUILD_TYPE} STREQUAL "release")
set(project_debug 0 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Release" PARENT_SCOPE)
else()
set(project_debug 1 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Debug" PARENT_SCOPE)
endif()
else()
set(project_debug 1 PARENT_SCOPE)
set(CMAKE_BUILD_TYPE "Debug" PARENT_SCOPE)
endif()
endif()
if(${project_debug})
message(STATUS "====> khl_common_set_project_debug : 编译模式: debug")
else()
message(STATUS "====> khl_common_set_project_debug : 编译模式: release")
endif()
endfunction()
<?xml version="1.0" encoding="UTF-8"?>
<personList>
<person id="1001"></person>
</personList>
\ No newline at end of file
#ifndef _KHL_MATH_H_
#define _KHL_MATH_H_
#include "khl_xml/khl_xml_node.h"
// C/C++ 跨平台时预处理判断平台环境
// 参考: https://www.cnblogs.com/Forgenvueory/p/12757271.html
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib,__FILE__"\\..\\..\\..\\lib\\khl_xmld.lib")
#else
#pragma comment(lib,__FILE__"\\..\\..\\..\\lib\\khl_xml.lib")
#endif
#else
//define something for Windows (32-bit only)
#endif
#elif __APPLE__
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
# error "Unknown Apple platform"
#endif
#elif __linux__
// linux
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif
#endif // _KHL_MATH_H_
#ifndef _KHL_XML_CONFIG_H_
#define _KHL_XML_CONFIG_H_
// C/C++ 跨平台时预处理判断平台环境
// 参考: https://www.cnblogs.com/Forgenvueory/p/12757271.html
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
#define DllExport __declspec(dllexport)
#else
//define something for Windows (32-bit only)
#endif
#elif __APPLE__
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
# error "Unknown Apple platform"
#endif
#elif __linux__
// linux
#define DllExport
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif
#endif // _KHL_XML_CONFIG_H_
#ifndef _KHL_XML_NODE_CONFIG_H_
#define _KHL_XML_NODE_CONFIG_H_
#include <string>
#include "khl_xml/khl_xml_config.h"
namespace khl
{
namespace xml
{
class DllExport XmlNode
{
public:
XmlNode();
~XmlNode();
std::string sayHello();
};
};
};
#endif // _KHL_XML_NODE_CONFIG_H_
\ No newline at end of file
add_subdirectory(main)
add_subdirectory(khl_xml)
\ No newline at end of file
# 头文件目录
include_directories(${CMAKE_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. XML_SRCS)
# 设置生成目录
if(${CMAKE_HOST_LINUX})
message(STATUS "====> linux lib dir")
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
endif()
# 生成动态库, 默认生成的是静态库
add_library(${PROJECT_NAME} SHARED ${XML_SRCS})
# 指定动态库版本
# VERSION 动态库版本
# SOVERSION API版本
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.0 SOVERSION 1)
cmake_language(CALL copy_dll_to_lib ${PROJECT_NAME})
#include "khl_xml/khl_xml_node.h"
namespace khl
{
namespace xml
{
XmlNode::XmlNode()
{
}
XmlNode::~XmlNode()
{
}
std::string XmlNode::sayHello()
{
return "hello khl xml";
}
};
};
\ No newline at end of file
# 头文件目录
include_directories(${CMAKE_SOURCE_DIR}/include ${tinyxml2_include_dir})
# 源文件列表
aux_source_directory(. MAIN_SRCS)
# 可执行文件目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
# 使用指定的源文件向项目添加可执行文件
add_executable(${khl_xml_main_name} ${MAIN_SRCS})
# tinyxml2
target_link_directories(${khl_xml_main_name} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(${khl_xml_main_name} ${tinyxml2_lib_name})
# khl_xml
if(${CMAKE_HOST_LINUX})
target_link_directories(${khl_xml_main_name} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(${khl_xml_main_name} ${PROJECT_NAME})
endif()
add_dependencies(${khl_xml_main_name} ${PROJECT_NAME})
cmake_language(CALL copy_dll_to_exe ${PROJECT_NAME})
#include <iostream>
#include <memory>
#include "tinyxml2.h"
#include "khl_xml/khl_xml.h"
int main()
{
std::string simpleXmlPath = "F:/2023/blog_code/tinyxml2-study/data/simple.xml";
tinyxml2::XMLDocument doc;
tinyxml2::XMLError xmlError = doc.LoadFile(simpleXmlPath.c_str());
if (tinyxml2::XMLError::XML_SUCCESS != xmlError)
{
std::cout << "xml 文件加载失败" << std::endl;
exit(0);
}
auto rootEl = doc.RootElement();
auto personEl = rootEl->FirstChildElement("person");
std::cout << personEl->Attribute("id") << std::endl;
std::cout << "-----------------" << std::endl;
auto node = std::make_unique<khl::xml::XmlNode>();
std::cout << node->sayHello() << std::endl;
std::cout << "-----------------" << std::endl;
std::cout << "khl xml" << std::endl;
return 0;
}
\ No newline at end of file
# 该项目所需 cmake 的最小版本, 如果 cmake 版本小于设置的版本, cmake 将停止处理并报错
cmake_minimum_required(VERSION 3.0)
# 设置项目名称和语言
project(khl_xml_test CXX)
# 指定 c++ 14
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(khl_common_set_project_debug)
message(STATUS "====> project_debug : ${project_debug}")
message(STATUS "====> CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
# 设置 debug 库后缀
if(${project_debug})
set(CMAKE_DEBUG_POSTFIX d)
endif()
# 消除警告 warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
# 参考: https://blog.csdn.net/five_east_west/article/details/125382251
if(${CMAKE_HOST_WIN32})
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif()
# 添加预处理器定义, 主要用于 windows
if(${project_debug})
add_compile_definitions(_DEBUG)
endif()
include_directories(${CMAKE_SOURCE_DIR}/third)
include_directories(${tinyxml2_include_dir})
include_directories(${CMAKE_SOURCE_DIR}/include)
aux_source_directory(. TEST_SRCS)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
add_executable(${PROJECT_NAME} ${TEST_SRCS})
# tinyxml2
target_link_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(${PROJECT_NAME} ${tinyxml2_lib_name})
# khl_xml
if(${CMAKE_HOST_LINUX})
target_link_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries(${PROJECT_NAME} ${khl_xml_lib_name})
endif()
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include "tinyxml2.h"
#include "khl_xml/khl_xml.h"
int add(int a, int b)
{
return a + b;
}
TEST_CASE("test 001")
{
CHECK(3 == add(1, 2));
}
TEST_CASE("test 002")
{
std::string simpleXmlPath = "F:/2023/blog_code/tinyxml2-study/data/simple.xml";
tinyxml2::XMLDocument doc;
tinyxml2::XMLError xmlError = doc.LoadFile(simpleXmlPath.c_str());
CHECK(tinyxml2::XMLError::XML_SUCCESS == xmlError);
auto rootEl = doc.RootElement();
auto personEl = rootEl->FirstChildElement("person");
const char * p = personEl->Attribute("id");
std::string str;
std::stringstream ss;
ss << p;
ss >> str;
CHECK("1001" == str);
}
TEST_CASE("test 003")
{
auto node = std::make_unique<khl::xml::XmlNode>();
std::cout << node->sayHello() << std::endl;
CHECK("hello khl xml" == node->sayHello());
}
此差异已折叠。
此差异已折叠。
# 头文件目录
include_directories(${tinyxml2_include_dir})
# 源文件列表
aux_source_directory(. TINYXML2_SRCS)
# 设置生成目录
# set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
if(${CMAKE_HOST_LINUX})
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
endif()
# 生成库, 默认生成的是静态库
add_library(${tinyxml2_lib_name} ${TINYXML2_SRCS})
if(${CMAKE_HOST_WIN32})
add_custom_command(TARGET ${tinyxml2_lib_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/build/${tinyxml2_lib_name}/${CMAKE_BUILD_TYPE}/${tinyxml2_lib_full_name} ${CMAKE_SOURCE_DIR}/lib/${tinyxml2_lib_full_name})
endif()
\ No newline at end of file
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册