Windows 10下编译Google GMOCK库libgmock.a的问题求助
Hey there, let's walk through getting GMOCK compiled smoothly on your Windows 10 system—you've hit two common pitfalls, but we can fix them easily.
First: Fixing the Cygwin CMake Path Error
Your initial CMake command failed because Windows-style backslashes get misinterpreted in Cygwin (they act as escape characters, which is why your path turned into C:UsersSETDownloads... instead of the correct one). Here's the correct approach:
Step-by-Step Cygwin/CMake Workflow
Double-check your source folder structure: Make sure you downloaded the full googletest repository (GMOCK is bundled with GTest, so you need both). Your extracted folder should look like this:
googletest-master/ ├── googlemock/ ├── googletest/ └── (other repo files)If you have nested
googlemock-masterfolders (like your path shows), move the innergooglemockandgoogletestfolders up one level to avoid confusion.Use Cygwin-friendly paths: Replace Windows backslashes (
\) with forward slashes (/), or use Cygwin's Unix-style path format (e.g.,/c/Users/Username/Downloads/instead ofC:\Users\Username\Downloads\).Follow CMake best practices (out-of-source build):
Open your Cygwin terminal and run these commands:# Navigate to the root of your googletest repo cd /c/Users/Username/Downloads/googletest-master # Create a separate build folder (keeps source clean) mkdir build cd build # Run CMake to generate Makefiles for GMOCK cmake -G "Unix Makefiles" ../googlemock # Compile the library makeIf you want to build both GTest and GMOCK together, replace the CMake command with:
cmake -G "Unix Makefiles" ..
Second: Fixing Visual Studio 2017 Dependency Errors
Opening the old gmock.sln directly fails because GMOCK relies on GTest headers, and the solution file doesn't automatically include GTest's path in your include directories. Instead, use CMake to generate a fresh Visual Studio project tailored to your environment:
Step-by-Step VS 2017 Workflow
- Ensure you have the full googletest repo (same folder structure as above).
- Open CMake GUI (install it if you haven't—CMake includes it by default when you select "Add to PATH" during installation).
- Configure paths:
- Set "Where is the source code" to your
googlemockfolder (e.g.,C:\Users\Username\Downloads\googletest-master\googlemock) - Set "Where to build the binaries" to a new folder (e.g.,
C:\Users\Username\Downloads\googletest-master\build-vs)
- Set "Where is the source code" to your
- Generate the VS project:
- Click "Configure", select "Visual Studio 2017" (match your architecture—Win32 or x64), then click "Finish".
- Once configuration completes without errors, click "Generate".
- Compile in VS:
- Open the generated
gmock.slnfile in Visual Studio 2017. - Set the build configuration (Debug/Release) and architecture, then build the
ALL_BUILDproject. CMake will automatically handle the GTest header dependencies, so you won't see thegtest-linked_ptr.herror anymore.
- Open the generated
Key Takeaways
- Always use forward slashes for paths in Cygwin/CMake (or Unix-style paths for Cygwin).
- GMOCK depends on GTest, so never compile GMOCK in isolation—ensure the GTest source is present and CMake can find it.
- For Visual Studio, always generate a fresh project with CMake instead of using old solution files, as repo structures and dependencies change over time.
内容的提问来源于stack exchange,提问作者HansPeterLoft

