error LNK2001: Linking error with VS C++ 6.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wizard1981
    New Member
    • Aug 2007
    • 11

    error LNK2001: Linking error with VS C++ 6.0

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

    Test1Dlg.obj : error LNK2001: unresolved external symbol "int __cdecl get_contact_sta te(enum CONTACT)" (?get_contact_s tate@@YAHW4CONT ACT@@@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!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Wizard1981
    When building my project, I'm getting following error:

    Test1Dlg.obj : error LNK2001: unresolved external symbol "int __cdecl get_contact_sta te(enum CONTACT)" (?get_contact_s tate@@YAHW4CONT ACT@@@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

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      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.

      Comment

      • Wizard1981
        New Member
        • Aug 2007
        • 11

        #4
        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.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          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_sta te 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?

          Comment

          • Wizard1981
            New Member
            • Aug 2007
            • 11

            #6
            Originally posted by oler1s
            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.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by Wizard1981
              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.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Wizard1981
                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

                Comment

                • Wizard1981
                  New Member
                  • Aug 2007
                  • 11

                  #9
                  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!

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by Wizard1981
                    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

                    Comment

                    • Wizard1981
                      New Member
                      • Aug 2007
                      • 11

                      #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

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Originally posted by Wizard1981
                        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.

                        Comment

                        • Wizard1981
                          New Member
                          • Aug 2007
                          • 11

                          #13
                          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.

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            Originally posted by Wizard1981
                            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

                            [code=c]
                            #ifdef __cplusplus
                            extern "C" { /* Declare declarations as C in a C++ file */
                            #endif /* __cplusplus */

                            /* Function prototypes etc */

                            #ifdef __cplusplus
                            }
                            #endif /* __cplusplus */

                            [/code]
                            in your header file

                            Comment

                            Working...