为什么静态链接时需要 *.obj 文件?
我不知道这是为什么。 我在多个项目中分发静态 *.lib,但此静态库生成许多 *.obj 文件。 看来我还需要将那些 *.obj 文件与 *.lib 一起分发。 否则,我会收到此错误:
1>LINK : fatal error LNK1181: cannot open input file 'nsglCore.obj'
这是为什么? 有没有办法将 *.obj 文件中的数据包含在 *.lib 中? 也许编译器中有一个开关?
这是我的静态库配置:
C/C++
/Od /GT /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_WINDOWS" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /MD /Yu"stdafx.hpp" /Fp"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\nsglCore-Win32-Release.pch" /Fo"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\\" /Fd"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\vc90-Release.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt
Librarian
/OUT:"e:\Development\Projects\nsGameLib\Source\Core\Output\nsglCore-Win32-Release.lib" /NOLOGO /LTCG
这是我使用静态库的项目配置:
C/C++
/O2 /Oi /I "E:\Development\Projects\nsGameLib\Samples\\DummyEngine\\" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_UNICODE" /D "UNICODE" /FD /EHsc /MD /Gy /Fo"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\\" /Fd"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\vc90-Release.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt
<强>链接器
/OUT:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Output\SampleOnlyCore-Win32-Release.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:"E:\Development\Projects\nsGameLib\Samples\..\Deployment\Libraries" /MANIFEST /MANIFESTFILE:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\SampleOnlyCore-Win32-Release.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\SampleOnlyCore-Win32-Release.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:PROMPT nsglCore kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
I'm not sure why is this. I'm distributing a static *.lib across multiple projects, but this static lib generates many *.obj files. Seems like I need to distribute also those *.obj files with the *.lib. Otherwise, I get this error:
1>LINK : fatal error LNK1181: cannot open input file 'nsglCore.obj'
Why is this? Is there a way to include the data in the *.obj files in the *.lib? Maybe a switch in the compiler?
This is my config for the static library:
C/C++
/Od /GT /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_WINDOWS" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /MD /Yu"stdafx.hpp" /Fp"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\nsglCore-Win32-Release.pch" /Fo"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\\" /Fd"e:\Development\Projects\nsGameLib\Source\Core\Intermediate\vc90-Release.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt
Librarian
/OUT:"e:\Development\Projects\nsGameLib\Source\Core\Output\nsglCore-Win32-Release.lib" /NOLOGO /LTCG
This is my config for the project using the static library:
C/C++
/O2 /Oi /I "E:\Development\Projects\nsGameLib\Samples\\DummyEngine\\" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_UNICODE" /D "UNICODE" /FD /EHsc /MD /Gy /Fo"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\\" /Fd"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\vc90-Release.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt
Linker
/OUT:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Output\SampleOnlyCore-Win32-Release.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:"E:\Development\Projects\nsGameLib\Samples\..\Deployment\Libraries" /MANIFEST /MANIFESTFILE:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\SampleOnlyCore-Win32-Release.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"e:\Development\Projects\nsGameLib\Samples\OnlyCore\Intermediate\SampleOnlyCore-Win32-Release.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:PROMPT nsglCore kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您的链接器行不正确。 该库应该有一个
.lib
后缀。 因此 nsglCore 应该是 nsglCore-Win32-Release.lib 或可能是 nsglCore-$(TargetPlatform)-$(ConfigurationName).lib 或其他任何内容正确的宏展开式是。I believe that your linker line is incorrect. The library should have a
.lib
suffix on it. SonsglCore
should bensglCore-Win32-Release.lib
or maybensglCore-$(TargetPlatform)-$(ConfigurationName).lib
or whatever the correct macro expansion is.一般来说,静态库不生成目标文件。 您要做的就是创建目标文件并将它们放入库中,然后链接器将在这些库中搜索目标文件。
我将从 UNIX 命令行的角度进行解释,因为这样更容易理解(在做基本的事情之前我不知道 VS 做了什么)。
用于创建可执行文件的示例命令行是:
第一行只是从您的
C
文件创建一个目标文件。 第二行通过将目标文件放在一起来创建可执行文件,通常遵循如下规则集:.o
文件都被链接。例如,假设您的
prog.c
包含行printf("hello\n");
。 这将导致您的prog.o
文件包含对printf
的引用,但该引用尚未满足。链接器将搜索您指定的库,直到满足该引用。 在这种情况下,它将搜索
/libdir/libstdc.ext
形式的所有文件,其中:/libdir
来自您的-L
选项(a搜索库的路径)。/lib
是一个常量。stdc
是要搜索的库的名称(来自-l
)。ext
是一个或多个扩展名(.a、.so、.sl
等)。一旦找到符号,就会链接该目标文件来解析它。 这可能会导致出现更多不满足的符号,例如
/libdir/libstdc.a(printf.o)
引用了/libdir/libstdc.a(putch .o)
。您的特定问题可能是由于您尝试直接链接目标文件而不是搜索库而引起的。 VS 应该有项目选项来指定目标文件、库搜索路径和库名称(我不确定最新版本是否如此,但我知道早期版本的 MSVC 确实如此)。
Generally, static libs do not generate object files. What you do is create object files and place them in a library, then the linker will search for the object files in those libraries.
I'll explain from a UNIXy command line point of view since that's the simpler to understande (I have no idea what machinations VS does before doing the basic stuff).
A sample command line for creating an executable is:
The first line simply creates an object file from your
C
file. The second line creates the executable by pulling object files together, generally following a rule set like:.o
file listed explicitly are linked.For example, say your
prog.c
contained the lineprintf("hello\n");
. That will have resulted in yourprog.o
file containing a reference toprintf
that is not yet satisfied.The linker will search your specified libraries until it satisfies that reference. In this case it will search all files of the form
/libdir/libstdc.ext
where:/libdir
is from your-L
option (a path to search for libraries in)./lib
is a constant.stdc
is the name of a library to search (from-l
).ext
is one or more extensions (.a, .so, .sl
, etc).Once the symbol is found, that object file is linked in to resolve it. This may result in more unsatisfied symbols appearing such as
/libdir/libstdc.a(printf.o)
having a reference to/libdir/libstdc.a(putch.o)
.Your particular problem may be caused by the fact that you're trying to link the object file directly rather than searching the libraries. VS should have project options to specify object files, library search paths and library names (I'm not sure of this for the latest versions but I know earlier versions of MSVC did).
啊... Visual Studio 对你来说太聪明了
转到包含该库的项目,右键单击属性
转到配置属性| 链接器
靠近底部的
:使用库依赖项输入 - 设置为否这是一个 Visual Studio 选项,用于直接获取 .obj 文件而不是 .lib 文件。 我想这是为了避免链接步骤,从而加快编译速度。
一般来说,您应该将创建 lib 文件的项目设置为使用它的项目的依赖项(在该属性窗口中的公共属性下)。 然后打开链接库依赖项。 这在大多数情况下效果很好。
Ah... Visual studio is being too clever for you
Go to the projects that include the lib, right click for properties
Goto Configuration Properties | Linker
Near the bottom : Use Library Dependancy Inputs - Set to No
This is a Visual Studio option to grab the .obj files directly rather than the .lib file. I imagine it is to avoid the link step and thus speed up the compile.
In general, you should set the project that creates the lib file as a dependancy of the project that uses it (under common properties in that properties window). You then turn on Link Library Dependancies. This works well in most cases.