Build instructions - CMake updates
- Dominant language
- C++
- Stars
- 1.8k
- Forks
- 342
- PR merge metrics
- No merged PRs in 30d
Description
Hi there,
I had a quick look at getting the sample application listed [here](https://bloomberg.github.io/bde/library_information/build.html#create-an-application) working, but ran into a couple of issues on macOS with the `CMakeLists.txt` file in its current form.
After building and installing the BDE libraries (to a local folder), I can configure correctly with the following command.
```bash
cmake -B build -G "Ninja Multi-Config" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_PREFIX_PATH=../bde/bde_install
```
However, when I invoke `cmake --build build` from my test project folder, I hit the following warnings and errors:
```
ld: warning: ignoring duplicate libraries: '/path/to/bde/bde_install/lib/libbdl.a', '/path/to/bde/bde_install/lib/libbsl.a'
ld: library 'rt' not found
```
As the library `bal` appears to depend on `bsl` and `bdl`, explicit references to them can both be removed (this stops the warning about duplicate libraries showing up), and from what I can tell `rt` is a Linux only library, so can be removed on macOS (and maybe a comment or a generator expression to only include that library on Linux can be added if needed).
This simplifies the CMakeLists.txt to the following...
```cmake
cmake_minimum_required(VERSION 3.15)
project(ball_init)
find_package(Threads REQUIRED)
find_package(bal REQUIRED)
add_executable(ball_init main.cpp)
target_link_libraries(ball_init PRIVATE bal Threads::Threads)
```
However with this, when I build, I hit the following error:
```
Undefined symbols for architecture arm64:
"BloombergLP::bsls_libraryfeatures_CPP14_ABI", referenced from:
BloombergLP::dummyAcCeSsbsls_libraryfeatures_assertion() in ball_init.cpp.o
"BloombergLP::ball::Log_Stream::Log_Stream(BloombergLP::ball::Category const*, bsl::basic_string_view> const&, int, int)", referenced from:
_main in ball_init.cpp.o
"BloombergLP::ball::BroadcastObserver::registerObserver(bsl::shared_ptr const&, bsl::basic_string_view> const&)", referenced from:
BloombergLP::ball::LoggerManager::registerObserver(bsl::shared_ptr const&, bsl::basic_string_view> const&) in ball_init.cpp.o
ld: symbol(s) not found for architecture arm64
c++: error: linker command failed with exit code 1 (use -v to see invocation)
```
The reason for this appears to be we're not explicitly setting the C++ version to match that of the one used when building the BDE libraries (it looks like on my machine at least, CMake defaults to C++14). To resolve this, the version of C++ needs to be specified with `target_compile_features`.
If for example, `eval bbs_build_env -u opt_64_cpp20` was used, then `cxx_std_20` would need to be set, if `eval bbs_build_env -u opt_64_cpp17` was used, then `cxx_std_17` would need to be set.
So the final CMakeLists.txt file looks like this:
```cmake
cmake_minimum_required(VERSION 3.15)
project(ball_init)
find_package(bal REQUIRED)
add_executable(ball_init ball_init.cpp)
target_link_libraries(ball_init PRIVATE bal)
target_compile_features(ball_init PRIVATE cxx_std_20)
```
I removed `Threads` as it also does not seem to be required for this example, and added the `target_compile_features` line. I also added the usage requirement (`PRIVATE`) to the `target_link_libraries` command that was missing before (this is a legacy CMake thing unfortunately, all newer `target...` commands enforce providing a usage requirement, be that `PRIVATE`, `PUBLIC`, or `INTERFACE`, but because `target_link_libraries` is really old it doesn't, as it predates the usage requirements).
If it's possible to update the documentation page with the content from the `CMakeLists.txt` file above that would be awesome. I would make a PR, but I'm not sure how to contribute changes to the documentation site. I'm afraid I haven't tested these changes on Linux, so it's possible `rt` and `Threads::Threads` are still needed there, but I think the other changes are still worthwhile adding.
Thanks very much for your time.
Contributor guide
Research direction
Start with the sample application instructions at the linked build.html#create-an-application page and compare its CMakeLists.txt example with the proposed version in this issue. Check whether the CMake changes work on both macOS and Linux, especially the handling of rt and Threads::Threads. Done means the documentation contains a working, correctly versioned CMake example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp
- Domain
- build-system, documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100