PhotonLibOS icon indicating copy to clipboard operation
PhotonLibOS copied to clipboard

Do I need to manually add source files like net/http/*.cpp to my CMakeLists.txt when using Photon?

Open TrKNguyen opened this issue 7 months ago • 9 comments

Hi, I'm trying to use Photon’s HTTP client in my project and I noticed that to use classes like photon::net::http::Client, I had to not only #include the header in my .cpp file, but also explicitly add the corresponding .cpp files from photon-src/net/http/ to my CMakeLists.txt, like this:

////

add_executable(client_https client_https.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/client.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/headers.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/message.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/body.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/url.cpp ${CMAKE_BINARY_DIR}/_deps/photon-src/common/estring.cpp )

////

Normally, I expect that after linking the Photon library via CMake and including the right headers, the implementation should already be compiled and linked. Like normally, when I want to use any file in /common, /io, /thread I just need to include the right headers. But in this case, I need to manually include the .cpp files from net/http.

👉 My questions:

Is this expected behavior for the HTTP client and related modules in Photon?

Are the net/http/*.cpp files intentionally not built into the main Photon static/shared library?

If I use other submodules like net/rsocket, will I also need to manually include their .cpp files?

I'm just trying to understand the intended integration model, whether those are optional utilities or meant to be built into the core Photon lib.

Thank you so much for your help.

TrKNguyen avatar May 31 '25 03:05 TrKNguyen

The current Photon CMake didn't separate modules into different libs, so my suggestion is to use libphoton.a or libphoton.so directly. https://photonlibos.github.io/docs/introduction/how-to-integrate

beef9999 avatar Jun 01 '25 13:06 beef9999

Is this expected behavior for the HTTP client and related modules in Photon?

No, it's not expected. One should need only to #include the header and link against libphoton.so/.a/.dylib.

Are the net/http/*.cpp files intentionally not built into the main Photon static/shared library?

They are always built and included in the library, as long as you use the provided CMakeLists.txt. There's no option for photon's HTTP implementation to be enabled or not.

If I use other submodules like net/rsocket, will I also need to manually include their .cpp files?

RSocket is optional. See below.

whether those are optional utilities or meant to be built into the core Photon lib.

There are some optional components in the build process. They are specified in CMakeLists.txt:

option(PHOTON_BUILD_TESTING "enable build testing" OFF)
option(PHOTON_BUILD_WITH_ASAN "build with asan" OFF)
option(PHOTON_ENABLE_URING "enable io_uring function" OFF)
set(PHOTON_ENABLE_FUSE "OFF" CACHE STRING "enable fuse version, OFF for disable, ON/2 for libfuse2, 3 for libfuse3")
option(PHOTON_GLOBAL_INIT_OPENSSL "Turn this off if any of your third-party libs inits old-version OpenSSL as well,
because Photon will register coroutine locks for crypto. But don't bother if you have latest OpenSSL >= 1.1.0" ON)
option(PHOTON_ENABLE_SASL "enable sasl" OFF)
option(PHOTON_ENABLE_MIMIC_VDSO "enable mimic vdso" OFF)
option(PHOTON_ENABLE_FSTACK_DPDK "Use f-stack + DPDK as the event engine" OFF)
option(PHOTON_ENABLE_EXTFS "enable extfs" OFF)
option(PHOTON_ENABLE_ECOSYSTEM "enable ecosystem" OFF)
option(PHOTON_ENABLE_RSOCKET "enable rsocket" OFF)
option(PHOTON_ENABLE_LIBCURL "enable libcurl" ON)
set(PHOTON_DEFAULT_LOG_LEVEL "0" CACHE STRING "default log level")
option(PHOTON_BUILD_OCF_CACHE "enable ocf cache" OFF)

lihuiba avatar Jun 04 '25 02:06 lihuiba

Hi,

Thanks for the clarification on the HTTP module, I’ve tried to follow the proper integration steps, but I still seem to hit the issue where I have to manually include .cpp files from net/http into my add_executable() call. I want to double-check whether I’m doing something wrong in my CMake setup.

Here’s how I build Photon:


rm -rf build

cmake -B build -D CMAKE_BUILD_TYPE=RelWithDebInfo \
-D PHOTON_BUILD_TESTING=ON \
-D PHOTON_BUILD_DEPENDENCIES=ON \
-D PHOTON_ENABLE_URING=ON \
-D PHOTON_AIO_SOURCE=https://pagure.io/libaio/archive/libaio-0.3.113/libaio-0.3.113.tar.gz \
-D PHOTON_ZLIB_SOURCE=https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz \
-D PHOTON_URING_SOURCE=https://github.com/axboe/liburing/archive/refs/tags/liburing-2.3.tar.gz \
-D PHOTON_CURL_SOURCE="" \
-D PHOTON_OPENSSL_SOURCE="" \
-D PHOTON_GFLAGS_SOURCE=https://github.com/gflags/gflags/archive/refs/tags/v2.2.2.tar.gz \
-D PHOTON_GOOGLETEST_SOURCE=https://github.com/google/googletest/archive/refs/tags/release-1.12.1.tar.gz

cmake --build build -j8

And this is the CMakeLists.txt in my project:


cmake_minimum_required(VERSION 3.14)
project(my_project)

set(PHOTON_ENABLE_URING ON CACHE INTERNAL "Enable io_uring")
set(PHOTON_CXX_STANDARD 14 CACHE INTERNAL "C++ standard")

include(FetchContent)
FetchContent_Declare(
    photon
    GIT_REPOSITORY https://github.com/alibaba/PhotonLibOS.git
    GIT_TAG main
)
FetchContent_MakeAvailable(photon)

add_executable(client_https
    client_https.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/client.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/headers.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/message.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/body.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/url.cpp
    ${CMAKE_BINARY_DIR}/_deps/photon-src/common/estring.cpp
)

target_link_libraries(client_https photon_static)

My questions:

  1. Since I'm already linking to photon_static, why do I still have to manually include the HTTP .cpp files?
  1. Am I misusing FetchContent_MakeAvailable() in this context? Is there a better or official way to ensure net/http is built into libphoton?
  1. Is it possible that when using FetchContent, the HTTP modules aren't being compiled into photon_static by default?

Thanks again — I really appreciate the support and would love to clean this up to avoid the manual file includes!

TrKNguyen avatar Jun 11 '25 03:06 TrKNguyen

  1. No you don't. You only need to link to photon_static.
  2. net/http is already in photon_static
  3. Same as above

add_executable(client_https client_https.cpp) target_link_libraries(client_https photon_static)

beef9999 avatar Jun 12 '25 03:06 beef9999

Thanks for your response! I understand that net/http should already be part of photon_static, and that normally just linking it like this:

add_executable(client_https client_https.cpp)
target_link_libraries(client_https photon_static)

It should be enough. However, I want to clarify that I did try this multiple times and still encountered linker errors like the following:

/home-local/tristan/PhotonLibOSTest/build/_deps/photon-src/include/photon/net/http/client.h:113: undefined reference to `photon::net::http::Request::Request(...)`
/home-local/tristan/PhotonLibOSTest/client_https.cpp:328: undefined reference to `photon::net::http::HeadersBase::insert(...)`
...
/usr/bin/ld: ... undefined reference to `photon::net::http::HeadersBase::insert(...)`
collect2: error: ld returned 1 exit status

These missing symbols all seem to be related to the HTTP module. The only way I’ve been able to fix the issue is by manually adding these .cpp files to my add_executable() target:

${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/client.cpp
${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/headers.cpp
${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/message.cpp
${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/body.cpp
${CMAKE_BINARY_DIR}/_deps/photon-src/net/http/url.cpp
${CMAKE_BINARY_DIR}/_deps/photon-src/common/estring.cpp

TrKNguyen avatar Jun 12 '25 04:06 TrKNguyen

What is your compiler and cmake version? Is it OK if you link to photon_shared?

lihuiba avatar Jun 12 '25 05:06 lihuiba

Toolchain:

  • GCC / g++ : 11.4.0 (Ubuntu 22.04)

  • CMake : 3.22.1

Then I tried to link against photon_shared instead of photon_static

add_executable(client_https client_https.cpp)
target_link_libraries(client_https photon_shared)

Result: the link still fails with the same HTTP-related symbols missing.

First few undefined symbols:

/include/photon/net/http/client.h:113: undefined reference to
    photon::net::http::Request::Request(...)
/client_https.cpp:328: undefined reference to
    photon::net::http::HeadersBase::insert(...)

TrKNguyen avatar Jun 12 '25 09:06 TrKNguyen

@TrKNguyen You may try using a container to compile you code.

docker pull ghcr.io/alibaba/photon-ut-base:latest

Our CI script is here.

https://github.com/alibaba/PhotonLibOS/blob/55ab58bfb1eee45f2e4b071c0cc6abb5e9a77923/.github/workflows/ci.linux.x86_64.yml#L177

beef9999 avatar Jun 13 '25 12:06 beef9999

Can you build and run the unit tests of photon in your environment? (by adding -D PHOTON_BUILD_TESTING=ON to cmake -B build, and ctest)

If the tests runs well, the shared library is OK, as the tests are referencing symbols in the lib.

lihuiba avatar Jun 16 '25 04:06 lihuiba