google / google/googletest

Use seperate symbol exporting macro in gmock

Open
#3,738 3 comments 0 reactions 1 assignee Claimed by @asoffer View on GitHub
bug
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

Currently `gtest` and `gmock` share the `GTEST_API_` symbol exporting macro which prevents building two separate Windows DLLs with one (`gmock`) depending on the other (`gtest`). You can see, for example, in the CMake build, that for Windows the `gmock` DLL simply includes the complete source code of `gtest`. While inefficient, more importantly, this prevents composing libraries that depend on `gmock`/`gtest` (since both libraries now provide symbols for `gtest`).

This is fairly easy to fix by making `gmock` use its own symbol exporting macro (`GMOCK_API_`). We've tried this in the `build2` package of `gmock` and the result works as expected (`gmock_all_test` pass). Here is the procedure:

1. Run the following command in `googlemock/` to replace `GTEST_API_` with `GMOCK_API_`:

```
find src include -name 'gmock*.h' -o -name 'gmock*.cc' | xargs -n 1 sed -i -e 's/GTEST_API_/GMOCK_API_/g'
```

(I've reviewed the result of this command on the 1.11.0 release, and all the replacements make sense.)

2. Copy the following fragment to `include/gmock/internal/gmock-port.h` before any existing macro definitions (this is adapted from `gtest-port.h`):

```c++
// GMOCK_API_ qualifies all symbols that must be exported. The definitions below
// are guarded by #ifndef to give embedders a chance to define GMOCK_API_ in
// gmock/internal/custom/gtest-port.h
#ifndef GMOCK_API_

#ifdef _MSC_VER
# if GMOCK_LINKED_AS_SHARED_LIBRARY
# define GMOCK_API_ __declspec(dllimport)
# elif GMOCK_CREATE_SHARED_LIBRARY
# define GMOCK_API_ __declspec(dllexport)
# endif
#elif __GNUC__ >= 4 || defined(__clang__)
# define GMOCK_API_ __attribute__((visibility ("default")))
#endif // _MSC_VER

#endif // GMOCK_API_

#ifndef GMOCK_API_
# define GMOCK_API_
#endif // GMOCK_API_
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.