CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2013
    Posts
    2

    A link error in C++

    Hello,

    I have a question about using Visual Studio to compile c++ codes.
    I created 3 projects: A, B and C. A and B were created as dynamic library, while C was created
    as static library. There are 3 classes, with one defined in each project.

    ClassA is defined in the project A:

    ClassA
    {
    public
    testA();
    };

    ClassB is defined in the project B.

    ClassB: public ClassC
    {
    public:
    testB();
    };

    ClassC is defined in the project C.
    ClassC
    {
    public:
    testC();

    };


    In Visual Studio, A has a dependency on B, and B has a dependency on C. But A doesn't have dependency on C.
    Also the implementation of ClassA::testA is:
    ClassA::testA ()
    {
    ClassB b;
    b.testC();
    }

    When I build project A, I got a link error which complained external symbol of testC cann't be found.
    How can I solve this issue without creating dependency between A and C?

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: A link error in C++

    As long as class B is hosted by dll, the dll is responsible for exporting everything that is called outside the dll.
    Best regards,
    Igor

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: A link error in C++

    See the sample.

    Code:
    J:\Temp\27>make.bat
    
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27c.cpp
    Microsoft (R) Library Manager Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27b.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:27b.exe
    /dll
    /out:27b.dll
    27c.lib
    27b.obj
       Creating library 27b.lib and object 27b.exp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27a.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:27a.exe
    /dll
    /out:27a.dll
    27b.lib
    27a.obj
       Creating library 27a.lib and object 27a.exp
    27a.obj : error LNK2019: unresolved external symbol "public: void __thiscall ClassC::testC(void)" (?testC@ClassC@@QAEXXZ) referenced in function "public: void __thiscall ClassA::testA(void)" (?testA@ClassA@@QAEXXZ)
    27a.dll : fatal error LNK1120: 1 unresolved externals
    Code:
    J:\Temp\27>make.bat b-exports-c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27c.cpp
    Microsoft (R) Library Manager Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27b.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:27b.exe
    /dll
    /out:27b.dll
    27c.lib
    27b.obj
       Creating library 27b.lib and object 27b.exp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27a.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:27a.exe
    /dll
    /out:27a.dll
    27b.lib
    27a.obj
       Creating library 27a.lib and object 27a.exp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    27x.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:27x.exe
    27x.obj
    27a.lib
    Attached Files Attached Files
    Last edited by Igor Vartanov; November 27th, 2013 at 02:31 AM.
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A link error in C++

    Quote Originally Posted by jeffwang66 View Post
    But A doesn't have dependency on C.
    This statement is incorrect.
    A is dependent on C because it uses ClassB which publicly derives from C and you are thus making direct use of an inherited class member of C. This requires you to have access to stuff in C.

    This is simply because of how inheritance works from a technical POV. It would seem you are assuming that deriving from B from C that the compiler "inserts" copies of the C member into B so they can be called as ClassB::testC while in reality ClassB::testC doesn't actually exist but is an alias the compiler allows to ClassC::testC.

    You can either make A dependant on C
    Or you can explicitely provide the
    ClassB::testC() { parentclass.testC(); }
    member.

    note that "parentclass" isn't a valid syntax, VC++ allows the nonstandard __super instead. Some other compilers have other means. the portable solution is "knowing" what your parent is and writing it as ClassC::testC();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured