如何在Visual Studio 2017中配置并使用zlib库?
Getting Zlib Working in Your Visual Studio 2017 Project
Hey there! You’ve already nailed the first step by adding the include directory—great job. Let’s wrap up the configuration and walk through how to actually use zlib with a simple example.
1. Set Up Additional Library Directories
First, head back to your project properties:
- Go to Linker > General > Additional Library Directories
- Add the path to zlib’s lib folder:
C:\Program Files\Zlib\msvc2015_64\lib
(Double-check the folder structure—your zlib zip should have alibsubfolder with the .lib files inside.)
2. Add Linker Dependencies
Next, tell the linker which library files to use:
- Navigate to Linker > Input > Additional Dependencies
- For Debug builds: Add
zlibd.lib(the debug version of the library) - For Release builds: Add
zlib.lib(the release version)
Note: Some zlib distributions might usezlibstatic.libfor static linking—check the filenames in yourlibfolder to be sure.
3. Fix Runtime DLL Access
Since you’re using the precompiled dynamic library version of zlib, your program needs the corresponding DLL files to run:
- Find the
binfolder in your zlib directory:C:\Program Files\Zlib\msvc2015_64\bin - Copy
zlibd1.dllto your project’s Debug output folder (where your .exe is built) - Copy
zlib1.dllto your project’s Release output folder - Alternatively, you can add the
binfolder to your system PATH, but copying the DLLs directly to your output directory is more reliable for portability.
4. Verify Platform Architecture
Make sure your project’s platform matches zlib’s architecture (64-bit):
- In Visual Studio’s toolbar, set the Solution Platform to
x64(not Win32). Mismatched platforms will cause linking errors.
Quick Test Example
Here’s a simple snippet to compress and decompress a string—this will confirm everything is set up correctly:
#include <iostream> #include <string> #include <cstring> #include <zlib.h> // Compress a string using zlib int compress_str(const std::string& input, std::string& output) { z_stream stream{}; if (deflateInit(&stream, Z_BEST_COMPRESSION) != Z_OK) { return -1; } stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(input.data())); stream.avail_in = input.size(); char buffer[32768]; int ret; do { stream.next_out = reinterpret_cast<Bytef*>(buffer); stream.avail_out = sizeof(buffer); ret = deflate(&stream, Z_FINISH); if (output.size() < stream.total_out) { output.append(buffer, stream.total_out - output.size()); } } while (ret == Z_OK); deflateEnd(&stream); return (ret == Z_STREAM_END) ? 0 : -1; } // Decompress a zlib-compressed string int decompress_str(const std::string& input, std::string& output) { z_stream stream{}; if (inflateInit(&stream) != Z_OK) { return -1; } stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(input.data())); stream.avail_in = input.size(); char buffer[32768]; int ret; do { stream.next_out = reinterpret_cast<Bytef*>(buffer); stream.avail_out = sizeof(buffer); ret = inflate(&stream, 0); if (output.size() < stream.total_out) { output.append(buffer, stream.total_out - output.size()); } } while (ret == Z_OK); inflateEnd(&stream); return (ret == Z_STREAM_END) ? 0 : -1; } int main() { const std::string original = "Hey zlib! Let's test compression with this sample text. Zlib is great for reducing data size!"; std::string compressed, decompressed; if (compress_str(original, compressed) == 0) { std::cout << "Original size: " << original.size() << " bytes\n"; std::cout << "Compressed size: " << compressed.size() << " bytes\n"; if (decompress_str(compressed, decompressed) == 0) { std::cout << "\nDecompressed text:\n" << decompressed << "\n"; } else { std::cerr << "Oops, decompression failed!\n"; return 1; } } else { std::cerr << "Oops, compression failed!\n"; return 1; } return 0; }
Troubleshooting Tips
- If you get "unresolved external symbol" errors: Double-check your library path and dependencies match the build mode (Debug/Release) and platform (x64).
- If the program crashes at runtime: Ensure the correct DLL is in the output folder (zlibd1.dll for Debug, zlib1.dll for Release).
内容的提问来源于stack exchange,提问作者Jayesh
相关产品推荐
相关产品推荐

