From 6ad50dc2859e9ea764aafe51b34d430a663fb1d3 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Mon, 23 Feb 2026 13:32:52 +0100 Subject: [PATCH] cmake: add C++ integration test Also: - include code to verify a C++-specific public header regression reported in 8.19.0-rc2. - curl/curl.h: mention C++ global namespace in comment. - GHA/dist: add CI job for C++. Runtime: 15 seconds. Follow-up to ee9b000438bd6e4cecc8a933804397a55ad0f7b8 #20686 Ref: #20682 Closes #20687 --- .github/workflows/distcheck.yml | 3 +++ Makefile.am | 1 + include/curl/curl.h | 4 +-- tests/cmake/CMakeLists.txt | 24 ++++++++++++------ tests/cmake/test.c | 2 +- tests/cmake/test.cpp | 44 +++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 tests/cmake/test.cpp diff --git a/.github/workflows/distcheck.yml b/.github/workflows/distcheck.yml index 21c958fdd9..eb461930f8 100644 --- a/.github/workflows/distcheck.yml +++ b/.github/workflows/distcheck.yml @@ -316,6 +316,9 @@ jobs: run: ./tests/cmake/test.sh add_subdirectory ${TESTOPTS} -DCURL_USE_OPENSSL=ON - name: 'via find_package' run: ./tests/cmake/test.sh find_package ${TESTOPTS} -DCURL_USE_OPENSSL=ON + - name: 'via find_package (C++)' + if: ${{ contains(matrix.image, 'ubuntu') }} + run: TEST_CMAKE_FLAGS=-DTEST_CPP=ON ./tests/cmake/test.sh find_package ${TESTOPTS} -DCURL_USE_OPENSSL=ON - name: 'via ExternalProject (old cmake)' if: ${{ contains(matrix.image, 'ubuntu') }} diff --git a/Makefile.am b/Makefile.am index 381ec987f3..1913f5d007 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,7 @@ CMAKE_DIST = \ CMakeLists.txt \ tests/cmake/CMakeLists.txt \ tests/cmake/test.c \ + tests/cmake/test.cpp \ tests/cmake/test.sh EXTRA_DIST = CHANGES.md COPYING RELEASE-NOTES Dockerfile .clang-tidy.yml .editorconfig $(CMAKE_DIST) diff --git a/include/curl/curl.h b/include/curl/curl.h index 52b51c4888..923b6f5473 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -3329,8 +3329,8 @@ CURL_EXTERN CURLcode curl_easy_ssls_export(CURL *handle, #if defined(__STDC__) && (__STDC__ >= 1) /* This preprocessor magic that replaces a call with the exact same call is only done to make sure application authors pass exactly three arguments - to these functions. Use recursive macros to allow reusing these symbols - as C++ method names. */ + to these functions. Use recursive macros to allow using these symbols via + the C++ global namespace '::' or reuse them as method names. */ #define curl_easy_setopt(handle, opt, param) \ curl_easy_setopt(handle, opt, param) #define curl_easy_getinfo(handle, info, arg) \ diff --git a/tests/cmake/CMakeLists.txt b/tests/cmake/CMakeLists.txt index 2c3d0daeb8..684759a4e6 100644 --- a/tests/cmake/CMakeLists.txt +++ b/tests/cmake/CMakeLists.txt @@ -25,7 +25,15 @@ cmake_minimum_required(VERSION 3.7...3.16 FATAL_ERROR) message(STATUS "Using CMake version ${CMAKE_VERSION}") -project(test-consumer C) +option(TEST_CPP "Test C++" OFF) + +if(TEST_CPP) + project(test-CPP-consumer CXX) + set(_source "test.cpp") +else() + project(test-C-consumer C) + set(_source "test.c") +endif() option(TEST_INTEGRATION_MODE "Integration mode" "find_package") @@ -93,7 +101,7 @@ elseif(TEST_INTEGRATION_MODE STREQUAL "ExternalProject") ${CURL_TEST_OPTS} BUILD_BYPRODUCTS "${_curl_static_lib}") - add_executable(test-consumer-static-fetch "test.c") + add_executable(test-consumer-static-fetch "${_source}") add_dependencies(test-consumer-static-fetch curl-external) if(WIN32) target_compile_definitions(test-consumer-static-fetch PRIVATE "CURL_STATICLIB") @@ -107,26 +115,26 @@ if(TEST_INTEGRATION_MODE STREQUAL "find_package" OR TEST_INTEGRATION_MODE STREQUAL "add_subdirectory" OR TEST_INTEGRATION_MODE STREQUAL "FetchContent") - add_executable(test-consumer-static-ns "test.c") + add_executable(test-consumer-static-ns "${_source}") target_link_libraries(test-consumer-static-ns PRIVATE "CURL::libcurl_static") - add_executable(test-consumer-shared-ns "test.c") + add_executable(test-consumer-shared-ns "${_source}") target_link_libraries(test-consumer-shared-ns PRIVATE "CURL::libcurl_shared") # Alias for either shared or static library - add_executable(test-consumer-selected-ns "test.c") + add_executable(test-consumer-selected-ns "${_source}") target_link_libraries(test-consumer-selected-ns PRIVATE "CURL::libcurl") endif() if(TEST_INTEGRATION_MODE STREQUAL "add_subdirectory" OR TEST_INTEGRATION_MODE STREQUAL "FetchContent") - add_executable(test-consumer-static-bare "test.c") + add_executable(test-consumer-static-bare "${_source}") target_link_libraries(test-consumer-static-bare PRIVATE "libcurl_static") - add_executable(test-consumer-shared-bare "test.c") + add_executable(test-consumer-shared-bare "${_source}") target_link_libraries(test-consumer-shared-bare PRIVATE "libcurl_shared") - add_executable(test-consumer-selected-bare "test.c") + add_executable(test-consumer-selected-bare "${_source}") target_link_libraries(test-consumer-selected-bare PRIVATE "libcurl") endif() diff --git a/tests/cmake/test.c b/tests/cmake/test.c index acc4953d1d..75c6bf913e 100644 --- a/tests/cmake/test.c +++ b/tests/cmake/test.c @@ -27,7 +27,7 @@ int main(int argc, const char **argv) { (void)argc; - puts("libcurl test:"); + puts("libcurl C test:"); puts(argv[0]); puts(curl_version()); puts("---"); diff --git a/tests/cmake/test.cpp b/tests/cmake/test.cpp new file mode 100644 index 0000000000..71747949a0 --- /dev/null +++ b/tests/cmake/test.cpp @@ -0,0 +1,44 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ +#include +#include + +class CurlClass { +public: + void curl_multi_setopt(void *a, int b, long c) { + std::cout << curl_version() << std::endl; + } +}; + +int main(int argc, const char **argv) +{ + (void)argc; + std::cout << "libcurl C++ test:" << std::endl; + std::cout << argv[0] << std::endl; + CurlClass mycurl; + mycurl.curl_multi_setopt(nullptr, 0, 0); + ::curl_easy_setopt(nullptr, CURLOPT_URL, "https://example.com/"); + std::cout << "---" << std::endl; + return 0; +}