|
-
October 29th, 2008, 05:33 AM
#1
LNK2019 error
Hi everyone, new here and hoping for some advice with the following problem. I only write code in a "need to know basis" way, and am very naive about the extensive compiler options and preprocessor stuff that goes on.
Summary: I am trying to use some downloaded libraries in some code I am working on; I thought I'd properly included the files but the linker is giving me a 2019 error.
For simplicity I am presently just trying to compile the sample code that came with one of the libraries (though I'm having the same problem with another library as well). Specifically, the library is for high-precision arithmetic and is available here (QD, second package from the top): http://crd.lbl.gov/~dhbailey/mpdist/
I'm using Visual c++ 2008 express edition. I put the /src files from the library in a folder, made sure the compiler knew to look there for headers (Tools -> Options -> Projects and Solutions -> VC++ directories, and added the path to the list of include files), opened a "test" solution and added all the files in /src to it's "header" directory (VC++ has a little "solution explorer" window for doing this; I'm not sure why it's necessary if the files are properly #included anyway). Then I tried to compile the following code, which was in the library's documentation:
Code:
#include <iostream>
#include <qd/qd_real.h>
using namespace std;
int main() {
qd_real a, b, c;
cout << "Enter a:";
cin >> a;
cout << "Enter b:";
cin >> b;
c = a + b;
cout << "a + b = " << c << endl;
cout << "sin(c) = " << sin(c) << endl;
return 0;
}
Should be straightforward enough, but when I try to build I get the following errors:
Code:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Linking...
1>test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct qd_real __cdecl sin(struct qd_real const &)" (__imp_?sin@@YA?AUqd_real@@ABU1@@Z) referenced in function _main
1>test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct qd_real const &)" (__imp_??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABUqd_real@@@Z) referenced in function _main
1>test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct qd_real __cdecl operator+(struct qd_real const &,struct qd_real const &)" (__imp_??H@YA?AUqd_real@@ABU0@0@Z) referenced in function _main
1>test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,struct qd_real &)" (__imp_??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAUqd_real@@@Z) referenced in function _main
1>test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall qd_real::qd_real(void)" (__imp_??0qd_real@@QAE@XZ) referenced in function _main
1>C:\Documents and Settings\Customer.CUSTOMER2007\My Documents\Visual Studio 2008\Projects\test\Debug\test.exe : fatal error LNK1120: 5 unresolved externals
1>Migdal - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm not sure what any of this really means and expect it's a subtlety related to the preprocessor or to the library needing to be updated for VC++ 2008 or something.
Any helpful thoughts? I've already tried playing with the Linker subsystem (switching from "windows" to "console" or back), which didn't help.
Thanks so much for taking the time to look.
-
October 29th, 2008, 06:58 AM
#2
Re: LNK2019 error
You did everything except including library files in your project.
Go Project->Properties->linker->input. And name the library files.
Cheers.
You just divided by zero, didn't you?
-
October 29th, 2008, 07:56 PM
#3
Re: LNK2019 error
Thanks for the quick reply; seems to be on the right track but I couldn't fix the problem.
I went to Project->Properties->linker->input and tried typing the .h files into the "additional dependencies" field. It wasn't obvious what path it would need; with no path or a local path I got:
Code:
LINK : fatal error LNK1104: cannot open file 'qd_real.h'
And if I give the full path I get:
Code:
fatal error LNK1107: invalid or corrupt file: cannot read at 0x16B7F
I think I'm confused about the distinction between headers and library files. The package I downloaded had a /src directory as well with several c++ source files with similar names to the .h files, but trying to include these in the "additional dependencies" field had the same result as above.
Incidentally, does anyone have a reading recommendation for learning about this aspect of coding? I hate to feel like I'm taking people's time with such basic issues.
-
October 29th, 2008, 08:03 PM
#4
Re: LNK2019 error
 Originally Posted by indirac
I think I'm confused about the distinction between headers and library files.
Indeed, good diagnosis. There are two separate build steps: Compiling and linking. H files are only necessary for compiling; they tell the compiler how functions can be called. The linker needs to know what the functions do.
For that, you have two options:
1) Add all source files of the library to your project (as source files). This is occasionally the way to go when the library you're trying to use is small and basic.
2) Compile the library (if it doesn't come with binaries) to produce library files. There should be instructions with the library on how to do this. On Windows, library files end in .lib; on Unix-based systems, they end in .a. You then need to link against the .libs or .as.
There are also sometimes dynamic libraries (.dll on Windows, .so on Unix-based) which may be called from the .libs or .as. In this case you may need to adjust your PATH environment variable or copy the dynamic library to the application's folder. Don't worry about this unless you get "dll cannot be found" or similar messages when you try to *run* the program (it won't show up at compile time).
If it helps any, consider: After compiling and before linking, the intermediate files you have are called object files, and typically end in .o or .obj. A .lib (or .a) is just a way of packaging up lots of object files in a single file. (The a in .a stands for "archive", incidentally.)
Last edited by Lindley; October 29th, 2008 at 08:07 PM.
-
October 30th, 2008, 05:18 AM
#5
Re: LNK2019 error
Thanks, that was very helpful and it's obvious I'll need to get a book or take a class at some point to properly learn all this.
After including all the source files (the package didn't come with a .lib and had no information on compiling a library aside from a discussion of options for the configuring script and the makefile, which are outside my knowledge anyway) I get 117 warnings about "inconsistent dll linkage" (why is a dll even involved if I haven't compiled a library?), and 36 errors of the form
Code:
error C2491: 'polyroot' : definition of dllimport function not allowed
So it appears something is still not linking properly. When you said "include them as source files," is there more to do than just including them in vc++'s little "source" directory in its solution explorer? Do they need to be in a certain location?
Again, if I'm being too bothersome as a new member by asking too basic questions, I'd love if someone could point me to their favorite book or web tutorial on this topic.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|