Skip to content

C++ setup #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Feb 27, 2025
Merged

C++ setup #246

merged 27 commits into from
Feb 27, 2025

Conversation

jtbandes
Copy link
Member

@jtbandes jtbandes commented Feb 26, 2025

Changelog

None

Docs

None

Description

Some initial setup for C/C++ builds. Overall structure is:

  • c folder with a Rust crate that builds a staticlib, cdylib, and uses cbindgen to generate a C header
  • cpp folder with a C++ wrapper around the C library, as well as tests and examples
    • Includes CMake scripts to build the tests and examples
    • Currently not planning to ship a prebuilt C++ library — users will have to link against the static/shared library and build the C++ sources in their own project.
  • CI workflow that builds the static & shared libraries on several platforms and uploads them to action artifacts

The actual functionality is limited to constructing/starting/stopping a server. Will continue to add functionality in future PRs.

)

add_library(foxglove STATIC "${foxglove_srcs}")
set_property(TARGET foxglove PROPERTY CXX_STANDARD 20)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using features which require C++20? I would have expected C++14 would be sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2020 was already 5 years ago, do you think it's still too early to move forward with it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷 my experience is c/c++ moves a lot slower. Would having this make it hard to incorporate an older library into a build, or is it possible to mix and match?

When I was building the C++ runtime of a project a couple years ago, I remember deciding on 14 because it included enough of the modern stuff I wanted, and there weren't major features after that I actually needed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is as opposed to cmake version, where I found a huge benefit to requiring newer cmake)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downgraded to 17

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 17 is a good compromise

include:
- runner: ubuntu-24.04
target: aarch64-unknown-linux-gnu
lib_name: libfoxglove_c.a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on calling it just libfoxglove instead of libfoxglove_c?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what should the C++ library be named then?

Copy link
Contributor

@amacneil amacneil Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grok and chatgpt suggested calling the c library libfoxglove and the c++ library libfoxglove++ or libfoxglove-cpp. They didn't think a suffix for the c variant was needed.

Not sure how I feel about special characters in the file name - it seems reasonably common (libxml++) but safer to avoid?

🤷 I don't feel strongly, feel free to stick to what you have if you prefer it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do libfoxglove_c and libfoxglove_cpp for consistency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be somewhat moot as I'm currently not planning to ship prebuilt c++ libraries; people will just integrate it into their own build system. I stuck with the foxglove_c name, but currently the CI artifacts also include the target in the filename so they don't conflict. We can change this later if we clarify our distribution needs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of just libfoxglove. The fact that it's C is fairly obvious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone did want to build the c++ version into a shared library and install it, what would they call that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libfoxglove_cpp or something like that, but optimize for the happy path

@jtbandes jtbandes marked this pull request as ready for review February 27, 2025 17:52
@jtbandes jtbandes requested a review from eloff February 27, 2025 17:53
@eloff
Copy link
Contributor

eloff commented Feb 27, 2025

  • Currently not planning to ship a prebuilt C++ library — users will have to link against the static/shared library and build the C++ sources in their own project.

So does this mean a header-only C++ library, that links to the C staticlib/dylib?

Copy link
Contributor

@eloff eloff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some minor bike-shedding type comments

* # Safety
* `name` and `host` must be null-terminated strings with valid UTF8.
*/
struct foxglove_websocket_server *foxglove_server_start(const char *name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will quickly get unwieldy, it would be better to pass in a C version of the ServerOptions struct. Don't have to address it now.

Maybe call this foxglove_start_server (verb first)

/**
* Stop and shut down a server.
*/
void foxglove_server_stop(struct foxglove_websocket_server *server);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe foxglove_stop_server (verb first?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re verb first, my thinking was as we add more and more types, it’ll be nice to have related methods sorted near each other so that’s why I was thinking [type]_[method]

Copy link
Contributor

@eloff eloff Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point. Carry on.

I forget it's like a poor man's namespace in C

@jtbandes
Copy link
Member Author

So does this mean a header-only C++ library, that links to the C staticlib/dylib?

Currently it includes cpp sources too, we could make it header-only but I’m not sure there’s much advantage to that?

@eloff
Copy link
Contributor

eloff commented Feb 27, 2025

So does this mean a header-only C++ library, that links to the C staticlib/dylib?

Currently it includes cpp sources too, we could make it header-only but I’m not sure there’s much advantage to that?

I seem to recall header-only libs being easier to setup in my build files, but that was a long time ago.

One less step, I'm not sure it matters that much.

@eloff
Copy link
Contributor

eloff commented Feb 27, 2025

🚢 it

@jtbandes jtbandes merged commit 69b9eb9 into main Feb 27, 2025
36 checks passed
@jtbandes jtbandes deleted the jacob/cpp-setup branch February 27, 2025 22:37
@bryfox bryfox mentioned this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants