Skip to content

Commit 7bed243

Browse files
committed
Address remaining Copilot review feedback
- Add OUTPUT_NAME property so libraries are named correctly: * libcpp_dotenv.a (static) instead of libcpp_dotenv_static.a * libcpp_dotenv.so/.dylib (shared) instead of libcpp_dotenv_shared.so - Enable CMAKE_POSITION_INDEPENDENT_CODE when building shared libraries to ensure static dependencies are compiled with -fPIC - Document BUILD_BOTH_LIBRARIES option in README.md with usage examples The OUTPUT_NAME property sets only the base name; CMake automatically adds the correct platform-specific extension (.so on Linux, .dylib on macOS).
1 parent 5222c99 commit 7bed243

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ if(BUILD_BOTH_LIBRARIES AND BUILD_SHARED_LIBS)
2121
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
2222
endif()
2323

24+
# When building shared libraries that link against static libraries,
25+
# we need position-independent code for the static libraries
26+
if(BUILD_SHARED_LIBS OR BUILD_BOTH_LIBRARIES)
27+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
28+
endif()
29+
2430
#------------------- SUBDIRECTORY ADDITION ------------------------------------
2531

2632
add_subdirectory(common)
@@ -53,6 +59,8 @@ if(BUILD_BOTH_LIBRARIES)
5359
)
5460
target_include_directories(cpp_dotenv_static PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
5561
target_compile_options(cpp_dotenv_static PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
62+
# Set output name to libcpp_dotenv.a (instead of libcpp_dotenv_static.a)
63+
set_target_properties(cpp_dotenv_static PROPERTIES OUTPUT_NAME cpp_dotenv)
5664

5765
# Shared library
5866
add_library(cpp_dotenv_shared SHARED ${CPP_DOTENV_SRC})
@@ -62,6 +70,8 @@ if(BUILD_BOTH_LIBRARIES)
6270
)
6371
target_include_directories(cpp_dotenv_shared PUBLIC ${CPP_DOTENV_INCLUDE_DIRS})
6472
target_compile_options(cpp_dotenv_shared PRIVATE ${CPP_DOTENV_COMPILE_OPTIONS})
73+
# Set output name to libcpp_dotenv.so/.dylib (instead of libcpp_dotenv_shared.so)
74+
set_target_properties(cpp_dotenv_shared PROPERTIES OUTPUT_NAME cpp_dotenv)
6575

6676
# Alias for backward compatibility:
6777
# When BUILD_BOTH_LIBRARIES is ON, the cpp_dotenv target is an alias to

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Supported build methods are:
4343

4444
### CMake
4545

46-
**cpp-dotenv** comes with support for `CMake` right out of the box. In order to use it, simply include this repository's directory and link the `cpp_dotenv` target to your own targets where needed:
46+
**cpp-dotenv** comes with support for `CMake` right out of the box (>=3.16). In order to use it, simply include this repository's directory and link the `cpp_dotenv` target to your own targets where needed:
4747

4848
```cmake
4949
add_subdirectory(cpp-dotenv)
@@ -55,6 +55,42 @@ target_link_libraries(YOUR_TARGET cpp_dotenv)
5555

5656
After this, you might use the library as described in [usage](#usage); no extra scoping, no need to worry about the project's directory structure.
5757

58+
#### Build Options
59+
60+
By default, only one library target named `cpp_dotenv` is built. The type of library (static or shared) is controlled by CMake's standard `BUILD_SHARED_LIBS` option:
61+
62+
```bash
63+
# Build static library (default)
64+
cmake -DBUILD_SHARED_LIBS=OFF ..
65+
66+
# Build shared library
67+
cmake -DBUILD_SHARED_LIBS=ON ..
68+
```
69+
70+
#### Building Both Static and Shared Libraries
71+
72+
To build both static and shared libraries in a single build directory, use the `BUILD_BOTH_LIBRARIES` option:
73+
74+
```bash
75+
cmake -DBUILD_BOTH_LIBRARIES=ON ..
76+
```
77+
78+
This creates two separate library targets:
79+
- `cpp_dotenv_static` - outputs as `libcpp_dotenv.a`
80+
- `cpp_dotenv_shared` - outputs as `libcpp_dotenv.so` (or `.dylib` on macOS)
81+
82+
For backward compatibility, the `cpp_dotenv` target remains available as an alias to `cpp_dotenv_static`. If you need to link against the shared library, explicitly specify `cpp_dotenv_shared`:
83+
84+
```cmake
85+
# Link against static library (default)
86+
target_link_libraries(YOUR_TARGET cpp_dotenv)
87+
88+
# Link against shared library
89+
target_link_libraries(YOUR_TARGET cpp_dotenv_shared)
90+
```
91+
92+
**Note:** When `BUILD_BOTH_LIBRARIES` is enabled, `BUILD_SHARED_LIBS` is ignored. Do not use both options simultaneously.
93+
5894
## Usage
5995

6096
To be able to use the dotenv classes, simply include the main header file:

0 commit comments

Comments
 (0)