By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

error LNK2001: Linking error with VS C++ 6.0

P: 11
When building my project, I'm getting following error:

Test1Dlg.obj : error LNK2001: unresolved external symbol "int __cdecl get_contact_state(enum CONTACT)" (?get_contact_state@@YAHW4CONTACT@@@Z)
Debug/Test1.exe : fatal error LNK1120: 1 unresolved externals

I have included the function by it's header in Test1Dlg.cpp so it should work. There is no error if I dont call the function.
The directory of the header file is known to the project, the other files would link without any problem.

Any idea what could be the problem would be helpful!
Aug 14 '07 #1
Share this Question
Share on Google+
13 Replies


Expert 10K+
P: 11,448
When building my project, I'm getting following error:

Test1Dlg.obj : error LNK2001: unresolved external symbol "int __cdecl get_contact_state(enum CONTACT)" (?get_contact_state@@YAHW4CONTACT@@@Z)
Debug/Test1.exe : fatal error LNK1120: 1 unresolved externals

I have included the function by it's header in Test1Dlg.cpp so it should work. There is no error if I dont call the function.
The directory of the header file is known to the project, the other files would link without any problem.

Any idea what could be the problem would be helpful!
Mentioning that function's prototype available just keeps the compiler's mouth shut.
There's still no guarantee though whether or not a compiled version of that function
exists and you have to make that compiled version available to the linker.

You didn't do that so the linker opened its mouth. If that function is stored in a
library you have to feed that library to the linker, otherwise if the compiled function
is just available in its own .o or .obj file you should feed that to the linker.

kind regards,

Jos
Aug 14 '07 #2

Expert 100+
P: 671
The header allows the program to compile. You are getting a linker error, which means you forgot to link the library in. You need to specify the library directory and the actual library that gets linked in.
Aug 14 '07 #3

P: 11
Thx for your help so far!
The function is NOT available via a library but as a obj-File located in the same dir than its header. Thats why Im expecting the linker to find it. Actually, all obj-Files are in the Debug-Folder where the compiler put them.
Aug 14 '07 #4

Expert 100+
P: 671
The function is NOT available via a library but as a obj-File located in the same dir than its header. Thats why Im expecting the linker to find it.
Header files aren't compiled. Only source files are. Besides, the include directories and source file directories are not relevant to the linker. Only where the intermediate and linkage time files are. If it so happens that they are in the same folder as the header or source files, well, ok.

Actually, all obj-Files are in the Debug-Folder where the compiler put them.
Object files? As in get_contact_state is a function you wrote? Did you properly write up the code in a source file, as opposed to merely declaring the existence of such a function?
Aug 14 '07 #5

P: 11
Did you properly write up the code in a source file, as opposed to merely declaring the existence of such a function?
Sure. I declared the function in a header, defined it in a source and included the header in the source-File using it (Test1Dlg). I compiled the source of the function independently with success so its obj-File is available in the Debug-Folder.
Aug 15 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
I compiled the source of the function independently with success so its obj-File is available in the Debug-Folder.
Independently? You mean outside of your normal project? If this is the case them the object will not be included on the link command which is why the symbol is unresolved, the linker can not find it in the objects and libraries it is linking.

You will have to either change the project linker settings to include the object as an input to the linker or add the C file to your project so that it just gets compiled and linked when you build the project normally.
Aug 15 '07 #7

Expert 10K+
P: 11,448
Thats why Im expecting the linker to find it.
A linker doesn't go hunting for the needed files. You have to explicitly supply it
with the files you want to have linked together. IDEs might hide this fact a bit
by doing it for you but linkers have other things to do; they don't hunt for, nor
find files for you.

kind regards,

Jos
Aug 15 '07 #8

P: 11
Maybe I should have mentioned that I wanted to link obj-Files compiled from c-sources... Now that I renamed all c-Files to cpp everything workes fine :)
Thank you anyway!
Aug 15 '07 #9

Expert 10K+
P: 11,448
Maybe I should have mentioned that I wanted to link obj-Files compiled from c-sources... Now that I renamed all c-Files to cpp everything workes fine :)
Thank you anyway!
Oh great. Next time please supply correct and sufficient information along with
your question. This forum is not for guessing games.

kind regards,

Jos
Aug 15 '07 #10

P: 11
Unfortunately, often it IS the problem to know which information is relevant and which not. If I guessed that the problem is with the c-Files, I propably had not posted this problem
Aug 15 '07 #11

weaknessforcats
Expert Mod 5K+
P: 9,197
Maybe I should have mentioned that I wanted to link obj-Files compiled from c-sources... Now that I renamed all c-Files to cpp everything workes fine :)
Thank you anyway!
This is not what you do to get C object files in your executable.

If you have the source code, add the .c files to your project as .c files. Do not make a copy of the code. Add the files from the locations where they normally reside. .c files are compiled as C and .cpp files are compiled as C++.

C++ is not C. By renaming, something that compiled in C may no longer compile in C++ and if you fix it to compile in C++ it may no longer compile in C.
Aug 15 '07 #12

P: 11
That is exactly what I wanted to do. But as I now know, obj-Files compiled from c are not compatible with cpp. You have to tell the compiler that you want to use a function defined in c explicitly.
Anyway, I'm just using quite basic c which should be compilable by an cpp compiler.
Aug 16 '07 #13

Banfa
Expert Mod 5K+
P: 8,916
You have to tell the compiler that you want to use a function defined in c explicitly.
Yes but it isn't very hard to put

Expand|Select|Wrap|Line Numbers
  1. #ifdef __cplusplus
  2. extern "C" {              /* Declare declarations as C in a C++ file  */
  3. #endif  /* __cplusplus */
  4.  
  5. /* Function prototypes etc */
  6.  
  7. #ifdef __cplusplus
  8. }
  9. #endif  /* __cplusplus */
  10.  
  11.  
in your header file
Aug 16 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.