Home > database >  Unresolved external symbol even if everything seems OK
Unresolved external symbol even if everything seems OK

Time:01-03

I'm not used to coding on Windows, and I am puzzled with this error on VS.

I am developping a static library libA, another one libB calling some methods in libA, and a program prog calling some methods in libB.

libA compiles without any problems.

Same for libB. The include and lib folders about libA are given to VS, as well as libA.lib as an additional dependency.

For prog, I give the include and lib folders concerning both libA and libB. The two libaries themselves are given as additional dependencies. It compiles, but while linking, I got a couple of errors LNK2001 of the type:

unresolved external symbol "public virtual void __thiscall libA::classA1::methodA1(void)" (?methodA1@classA1@libA@@UEAXXZ) in File libB.lib(classB1.obj)

I did a dumpbin of libB and the symbol ?methodA1@classA1@libA@@UEAXXZ appears many times in the dump file.

Dump of file .\libB.lib

File Type: LIBRARY

Archive member name at 8: /                
61C9303F time/date Mon Dec 27 12:17:19 2021
         uid
         gid
       0 mode   36987E size correct header end

14630 public symbols 

...
3F43C3C ?methodA1@classA1@libA@@UEAXXZ
...

Archive member name at 3698C2: /                
61C9303F time/date Mon Dec 27 12:17:19 2021
         uid
         gid
       0 mode   36272A size correct header end

61 offsets

...
32 ?methodA1@classA1@libA@@UEAXXZ
... 

SECTION HEADER #42 .text$mn name
       0 physical address
       0 virtual address
     1D7 size of raw data
    5FA3 file pointer to raw data (00005FA3 to 00006179)
    617A file pointer to relocation table
       0 file pointer to line numbers
       D number of relocations
       0 number of line numbers 60501020 flags
         Code
         COMDAT; sym= "public: virtual void __cdecl libA::classA1::methodA1(void)" (?methodA1@classA1@libA@@UEAAXXZ)
         16 byte align
         Execute Read

...

SECTION HEADER #92
  .xdata name
       0 physical address
       0 virtual address
      1C size of raw data
    6B4E file pointer to raw data (00006B4E to 00006B69)
    6B6A file pointer to relocation table
       0 file pointer to line numbers
       1 number of relocations
       0 number of line numbers
40301040 flags
         Initialized Data
         COMDAT; sym= $unwind$?methodA1@classA1@libA@@UEAAXXZ
         4 byte align
         Read Only

...

SECTION HEADER #93
  .pdata name
       0 physical address
       0 virtual address
       C size of raw data
    6B74 file pointer to raw data (00006B74 to 00006B7F)
    6B80 file pointer to relocation table
       0 file pointer to line numbers
       3 number of relocations
       0 number of line numbers
40301040 flags
         Initialized Data
         COMDAT; sym= $pdata$?methodA1@classA1@libA@@UEAAXXZ
         4 byte align
         Read Only

...

RELOCATIONS #93
                                                Symbol    Symbol
 Offset    Type              Applied To         Index     Name
 --------  ----------------  -----------------  --------  ------
 00000000  ADDR32NB                   00000000       124  $LN269
 00000004  ADDR32NB                   000001D7       124  $LN269
 00000008  ADDR32NB                   00000000       22A  $unwind$?methodA1@classA1@libA@@UEAAXXZ

...

RELOCATIONS #101
                                                Symbol    Symbol
 Offset    Type              Applied To         Index     Name
 --------  ----------------  -----------------  --------  ------
 00000000  ADDR64            00000000 00000000       3BA  ??_R4classA1@libA@@6B@ (const libA::classA1::`RTTI Complete Object Locator')
 00000008  ADDR64            00000000 00000000        D3  ??_EclassA1@libA@@UEAAPEAXI@Z (public: virtual void * __cdecl libA::classA1::`vector deleting destructor'(unsigned int))
 00000010  ADDR64            00000000 00000000        8D  _purecall
 00000018  ADDR64            00000000 00000000        8D  _purecall
 00000020  ADDR64            00000000 00000000        CE  ?methodA1@classA1@libA@@UEAAXXZ (public: virtual void __cdecl libA::classA1::methodA1(void))
 00000028  ADDR64            00000000 00000000        CF  ?methodA2@classA1@libA@@UEAAXXZ (public: virtual void __cdecl libA::classA1::methodA2(void))

...

0CE 00000000 SECT42 notype ()    External     | ?methodA1@classA1@libA@@UEAAXXZ (public: virtual void __cdecl libA::classA1::methodA1(void))

...

22A 00000000 SECT92 notype       Static       | $unwind$?methodA1@classA1@libA@@UEAAXXZ
22B 00000000 SECT93 notype       Static       | .pdata
    Section length    C, #relocs    3, #linenums    0, checksum AAAF74B1, selection    5 (pick associative Section 0x42)
22D 00000000 SECT93 notype       Static       | $pdata$?methodA1@classA1@libA@@UEAAXXZ
22E 00000000 SECT94 notype       Static       | .voltbl
    Section length    4, #relocs    0, #linenums    0, checksum D5442740, selection    5 (pick associative Section 0x42)

Same for the dumplib file of libA.

If it helps, here is the linker command for prog:

/OUT:"C:\prog\Release\prog.exe" /MANIFEST /LTCG:incremental /NXCOMPAT /PDB:"C:\prog\Release\prog.pdb" /DYNAMICBASE "libA.lib" "libB.lib" "some_other_libs.lib" /DEBUG /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO /PGD:"C:\prog\Release\prog.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Release\prog.exe.intermediate.manifest" /LTCGOUT:"Release\prog.iobj" /OPT:ICF /ERRORREPORT:PROMPT /ILK:"Release\prog.ilk" /NOLOGO /TLBID:1

CodePudding user response:

Finally, I found the problem.

libA and libB were correctly working on Linux, so I didn't understand why I got this problem on Windows.

Turns out that, I don't really know why, but the VS solution file generated by CMake allowed to compile libA with an x86 target architecture, but together with the additional option "/machine x64" in the command line section (Project Properties > Linker (or Librarian for a static library) > Command Line).

I removed this "/machine x64" option, recompiled libA, and now the whole thing compiles and links.

  • Related