简体   繁体   中英

Can I statically link ws2_32.lib in windows? c++ MFC

I'm trying to deploy a single .exe file with a GUI written in c++. Using microsoft visual studio.

I have created the GUI with MFC and I can get it to build statically by changing the build options to:

Platform Toolset - Visual Studio 2015 - Windows XP (v140_xp) Use of MFC - Use MFC in a Static Library Runtime Library - Multi-threaded (/MT)

This works fine and gives me a GUI that I can execute on Windows 7/XP from a single .exe file.

However, I need to add in socket support because the program contains an IRC client. Therefore I need to include the libraries:

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <boost/tokenizer.hpp>

However when I do this it will no-longer compile with: Runtime Library - Multi-threaded (/MT) instead it requires: Runtime Library - Multi-threaded Debug (/MTd)

Which means it is no-longer a portable .exe

Any ideas?

The Visual C++ compiler embeds a list of default libraries in each obj file it creates. During linking, these default library directives are processed and the libraries are implicitly added to the link step.

This means that if two obj files were compiled with different C-Runtime settings there will be multiple C-Runtime libs being included at link time, which will conflict.

Generally, the correct solution is to rebuild the sources with the same C-Runtime settings as code compiled with the expectation that it will be used with a particular runtime might fail if used against a different one.

However, you can simply force a resolution to the situation by adding the unwanted libraries to the "Ignore Specific Default Libraries" Linker setting for the project.

The trick is to know which actual libraries map to which settings.

The MSDN Use Runtime Library lists the various runtimes that can be selected, in your case, you want to exclude the static multithreaded debug runtime from being included in your build, so libcmtd.lib needs to be added to default libraries to ignore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM