Click to See Complete Forum and Search --> : Using a .lib from .NET app????
voorhees
November 8th, 2005, 04:09 PM
Hi,
I am working on a C++ .NET application that requires the use of a .lib resource file. A .h file is used to define various things and make the calls to the .lib. The code works fine until it gets to a line that requires the use of the .lib file and the build has problems. The line in the .cpp file is:
myprofile = open_profile(READ|SETENDIAN, plt_file, &err);
Where open_profile is defined in the .h file as:
extern PROFILE* open_profile ( int, char *,int * );
It compiles with no errors, however I get the link error:
Form1.obj : error LNK2001: unresolved external symbol "struct ProfileData * __cdecl open_profile(int,char *,int *)" (?open_profile@@$$FYAPAUProfileData@@HPADPAH@Z)
This to me looks like something that might be from the .lib file. So I think it is using the .lib file, but I am not sure. Any help would be very much appreciated with the syntax in calling the .lib file so that it links properly.
Thank you very much in advance.
NoHero
November 8th, 2005, 04:35 PM
The lib file is given to the linker so he can resolve external functions into the proper DLL/Static library. Just feed the linker with your lib file on two ways:
Rightclick your Project
Properties
Under "Options" go to the Linker section
Under "Input" type the full/relative path to your lib into the first text box
Or use the proper #pragma directive inside your code:
#pragma comment(lib, "mylib.lib")
voorhees
November 8th, 2005, 05:22 PM
Thank you very much for the reply; however it did not seem to fix my link error.
I included the pragma statement:
#pragma comment(lib, "PftkLib.lib")
but that did not help. I also tried the first option with no success. I right clicked on my project and went to the Linker section then into the Input section and placed the path (c:\temp\PftkLib.lib) in the Additional Dependencies box (the first box). Additionally, I tried putting the same path in the Additional Library Directories under the General option of the Linker option. Still with no luck. What could I be missing?
Thanks.
voorhees
November 8th, 2005, 05:43 PM
The title I orginally typed is misleading, however I can not figure out how to change it. It should be: Using a .lib in a .NET app????
I am trying to us a .lib file with my C++ VS.NET application. I was told the library file was created in VS.NET, and I believe it may have been writen in C. I have created a console application that uses it with no problems. However, I get the above link error in my .NET application.
Thanks for your help.
voorhees
November 8th, 2005, 07:26 PM
A little more information.
I looked at my build log and it appears to me that it is linking my .lib into the build. Below is the Command Lines section of the build log.
Creating temporary file "d:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\RSP000036.rsp" with contents
[
/Od /AI "D:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug" /D "WIN32" /D "_DEBUG" /D "_MBCS" /FD /EHsc /MTd /GS /Yu"stdafx.h" /Fp"Debug/TESTApp2.pch" /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /c /Zi /clr /TP /FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll"
/FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\System.dll"
/FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\System.Data.dll"
/FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\System.Drawing.dll"
/FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll"
/FU "c:\Winnt\Microsoft.NET\Framework\v1.1.4322\System.XML.dll"
".\Form1.cpp"
]
Creating command line "cl.exe @"d:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\RSP000036.rsp" /nologo"
Creating temporary file "d:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\RSP000037.rsp" with contents
[
/OUT:"D:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\TESTApp2.exe" /INCREMENTAL /NOLOGO /LIBPATH:"C:\temp" /DEBUG /ASSEMBLYDEBUG /PDB:"D:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug/TESTApp2.pdb" /SUBSYSTEM:WINDOWS /FIXED:No PftkLib.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /ASSEMBLYRESOURCE:".\Debug\TESTApp2.AboutBox.resources" /ASSEMBLYRESOURCE:".\Debug\TESTApp2.Form1.resources"
".\Debug\AboutBox.obj"
".\Debug\AssemblyInfo.obj"
".\Debug\Form1.obj"
".\Debug\stdafx.obj"
".\Debug\app.res"
"c:\temp\PftkLib.lib"
]
Creating command line "link.exe @"d:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\RSP000037.rsp""
I get a bunch of warnings for things I understand, and will be fixed if I get the .lib linked in properly, and then I get the link error.
Linking...
Form1.obj : error LNK2001: unresolved external symbol "struct ProfileData * __cdecl open_profile(int,char *,int *)" (?open_profile@@$$FYAPAUProfileData@@HPADPAH@Z)
D:\data\voorhees\My Documents\Visual Studio Projects\TESTApp2\Debug\TESTApp2.exe : fatal error LNK1120: 1 unresolved externals
Just thought I would pass this along if it would help anyone with the diagnosis of my problem.
Thanks.
NoHero
November 9th, 2005, 06:19 AM
Do you have any examples on this DLL/library you are using? Is it self written? You might have some type mismatches.
ahh... Is it a C library? If so you need the "extern "C"" directive for it. But your header file should do this automatically:
#ifdef __cplusplus // if this header gets included by C++:
extern "C" {
#endif
PROFILE* open_profile ( int, char *,int * );
#ifdef __cplusplus
}
#endif
This allows C++ to treat your C library/functions as C call and not as C++ call.
voorhees
November 9th, 2005, 10:45 AM
That looks to be it. It seems to work fine now. I of course need to fix some other things to get the program to function as intended, however this looks like it solves my biggest issue.
I put the "extern "C"" directive around all of my external calls in the header file and removed the extern from in front of each of them. When I built the solution I no longer got the link error.
Thank you very much for your help.
NoHero
November 9th, 2005, 11:39 AM
Thank you very much for your help.
You are very welcome...
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.