LNK2005当静态lib引用dll中的符号而静态lib尚未解析时 [英] LNK2005 when static lib references symbol in dll and static lib not already resolved

查看:87
本文介绍了LNK2005当静态lib引用dll中的符号而静态lib尚未解析时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下引出LNK2005。 奇怪的是,只有在需要解析静态库中的符号时才会发生这种情况,这个符号尚未在DLL和静态库中定义解析。 直接在最终目标
中使用此符号不会产生任何问题。链接包含相同符号的多个静态库似乎没有问题。 错误页面似乎没有描述问题:


https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools -error-lnk2005


要复制的文件或https://github.com/abellgithub/mslinkissue:


< g class =" gr_ gr_769 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling"数据-GR-ID =" 769" id =" 769"> json < / g> ..hpp


#pragma once zh-b $ b

__declspec(dllexport)int json(int i);

__declspec(dllexport )int json2(int i);


json.cpp


__ declspec (dllexport)int json(int i)

{

  &NBSP;返回i + 1;

}



__declspec(dllexport)int json2(int i)

{

  &NBSP;返回i + 1;

}


jsonuser.hpp


#pragma once zh-b $ b

__declspec(dllexport)int jsonuser(int i);


jsonuser.cpp


#include" json.hpp"



__declspec(dllexport)int jsonuser(int i)

{

return json(i)+ 1;

}


arbiter.hpp


#pragma once zh-b $ b

__declspec(dllexport)int arbiter(int i);


arbiter.cpp


int json(int);
$
int json2(int);



__declspec(< g class =" gr_ gr_875 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id =" 875" id =" 875" ;> dllexport< / g>)int arbiter(int i)

{

//这个之前没有引用过符号,并且在考虑因为
//< g class =" gr_ gr_876 gr-alert gr_spell gr_inline_cards gr_run_anim时,链接器会搜索库。仅使用ContextualSpelling -del replaceWithoutSep"数据-GR-ID =" 876" id =" 876">仲裁者< / g> 。LIB&NBSP;这导致LNK2005

  &NBSP; return json2(i)+ 1;

$
//用以下内容替换上面的内容会导致没有错误,我想因为链接器已经加载了¥b $ b //符号..

//  &NBSP; return json(i)+ 1;

$
}


mytest。 cpp


#include< iostream>
$


#include" json.hpp"

#include" jsonuser.hpp"

#include" arbiter.hpp"



int main()

{

int i = json(1);

i =仲裁者(i);

std :: cerr< < "I =" << i<< "!\ n"; $
返回0;

}


CMakeLists.txt


add_library(jsonlib STATIC json.cpp)



add_library(jsonuserlib SHARED jsonuser.cpp)

target_link_libraries(jsonuserlib

jsonlib)
$
add_library(arbiterlib STATIC arbiter.cpp)



add_executable(myprog

mytest.cpp)



target_link_libraries(myprog

-verbose

jsonuserlib

arbiterlib

jsonlib




解决方案


<感谢您在这里发帖。


>> LNK2005当静态lib dll和静态库中的引用符号尚未解析


请提供有关您的问题的更详细的错误信息。 


如果在不同库中的两个成员对象中使用不同的符号定义符号,并且使用了两个成员对象,则可能会发生此错误。静态链接库时解决此问题的一种方法是仅使用一个库中的成员对象
并首先在链接器命令行中包含该库。


除此之外,这个论坛是关于微软产品的c ++开发。对于与CMake更相关的案例,我建议您在下面的论坛发帖。


https ://cmake.org/get-involved/


您的理解与合作将不胜感激。


最诚挚的问候,


Baron Bi



The following elicits LNK2005.  What's strange is that it only seems to happen when needing to resolve a symbol in a static library not yet resolved that has been defined in a DLL and a static library.  Using this symbol directly in the final target causes no issue. Linking with multiple static libraries containing the same symbol seems to cause no problem.  The error page doesn't seem to describe the issue:

https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2005

Files to reproduce or https://github.com/abellgithub/mslinkissue :

<g class="gr_ gr_769 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="769" id="769">json</g>.hpp

#pragma once

__declspec(dllexport) int json(int i);
__declspec(dllexport) int json2(int i);

json.cpp

__declspec(dllexport) int json(int i)
{
    return i + 1;
}

__declspec(dllexport) int json2(int i)
{
    return i + 1;
}

jsonuser.hpp

#pragma once

__declspec(dllexport) int jsonuser(int i);

jsonuser.cpp

#include "json.hpp"

__declspec(dllexport) int jsonuser(int i)
{
return json(i) + 1;
}

arbiter.hpp

#pragma once

__declspec(dllexport) int arbiter(int i);

arbiter.cpp

int json(int);
int json2(int);

__declspec(<g class="gr_ gr_875 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="875" id="875">dllexport</g>) int arbiter(int i)
{
// This symbols hasn't been referenced before and causes the linker to search libraries when considering
// <g class="gr_ gr_876 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling only-del replaceWithoutSep" data-gr-id="876" id="876">the the arbiter</g> lib.  This causes LNK2005
    return json2(i) + 1;

// Replacing the above with the following causes no error, I suppose because the linker has already loaded
// the symbol..
//    return json(i) + 1;

}

mytest.cpp

#include <iostream>

#include "json.hpp"
#include "jsonuser.hpp"
#include "arbiter.hpp"

int main()
{
int i = json(1);
i = arbiter(i);
std::cerr << "I = " << i << "!\n";
return 0;
}

CMakeLists.txt

add_library(jsonlib STATIC json.cpp)

add_library(jsonuserlib SHARED jsonuser.cpp)
target_link_libraries(jsonuserlib
jsonlib)
add_library(arbiterlib STATIC arbiter.cpp)

add_executable(myprog
mytest.cpp)

target_link_libraries(myprog
-verbose
jsonuserlib
arbiterlib
jsonlib
)


解决方案

Hi,

thanks for posting here.

>>LNK2005 when static lib references symbol in dll and static lib not already resolved

Please provide more detailed error information about your issue. 

This error could occur if the symbol is defined differently in two member objects in different libraries, and both member objects are used. One way to fix this issue when the libraries are statically linked is to use the member object from only one library, and include that library first on the linker command line.

Besides, this forum is about c++ development with Microsoft products. For your case which is more related to CMake, I suggest you post on this forum below.

https://cmake.org/get-involved/

Your understanding and cooperation will be grateful.

Best Regards,

Baron Bi


这篇关于LNK2005当静态lib引用dll中的符号而静态lib尚未解析时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆