×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / help help! 怎么在一个EXE里连两个STATIC LIBRARY?这两个LIB SHARE一些FUNCTION,我联的时候VC报错:a.lib: function xyz is already defined in b.lib.
    • Your a.lib has a xyz function and your b.lib also has a xyz function with the same signature as that xyz in a.lib. Your linker did not know which xyz should be used.
      • that xyz is a inner function, not the one decleared in lib header file, actually they are class construcure/destructure and common used member functions inside the lib.
    • I think you can't have duplicate name of function in libs, contact your lib vender for a name change.
    • sample code is here~~~~~~~~
      本文发表在 rolia.net 枫下论坛enc lib: enc.h/codec.cpp/cod.h/cod.cpp with a define "_ENC_LIB_"
      dec lib: dec.h/codec.cpp/cod.h/cod.cpp with a define "_DEC_LIB_"
      exe: test.cpp/dec.h/enc.h

      VC reports link error with this code: cod::cod()/cod::~cod()/cod::read_file()
      already defined. but everything is ok when I delete #ifdef in cod.cpp/cod.h,
      why? anyway to resolve it if I really want those #ifdef? real source is
      really big so please dont tell me to duplicate functions.

      thanks,

      ///////////////////////////////////////////////////////
      //cod.cpp
      #include "stdio.h" #include "cod.h"
      cod::cod(){} cod::~cod(){} void cod::read_file(){ printf("cod::read_file\n");}
      #ifdef _DEC_LIB_ void cod::dec(){ printf("cod::dec\n");} #endif
      #ifdef _ENC_LIB_ void cod::enc(){ printf("cod::enc\n");} #endif

      //codec.cpp
      #include "codec.h" #include "cod.h"
      #ifdef _DEC_LIB_ dec::dec(){ pdec = new cod; ((cod *)pdec)->read_file();}
      dec::~dec(){ delete pdec;} void dec::decode(){ ((cod *)pdec)->dec();}
      #endif
      #ifdef _ENC_LIB_ enc::enc(){ penc = new cod; ((cod *)penc)->read_file();}
      enc::~enc(){ delete penc;} void enc::encode(){ ((cod *)penc)->enc();}
      #endif

      //dec.h
      #ifndef enc_h #define enc_h
      class enc{ public: void *penc; enc(); ~enc(); void encode();}; #endif

      //enc.h
      #ifndef enc_h #define enc_h
      class enc{ public: void *penc; enc(); ~enc(); void encode();}; #endif

      //test.cpp
      #include "enc.h" #include "dec.h"
      #pragma comment(lib, "enc.lib") #pragma comment(lib, "dec.lib")
      void main(){ enc e; e.encode(); dec d; d.decode();}更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Where is your cod.h? And please one line has one sentence.
        • create 2 project, put those defination in project/setting.
          本文发表在 rolia.net 枫下论坛//cod.h
          #ifndef cod_h
          #define cod_h
          class cod{
          public:
          cod();
          ~cod();
          void read_file();
          #ifdef _DEC_LIB_
          void dec();
          #endif
          #ifdef _ENC_LIB_
          void enc();
          #endif
          };
          #endif

          //cod.cpp
          #include "stdio.h"
          #include "cod.h"

          cod::cod()
          {
          }
          cod::~cod()
          {
          }
          void cod::read_file()
          {
          printf("cod::read_file\n");
          }
          #ifdef _DEC_LIB_
          void cod::dec()
          {
          printf("cod::dec\n");
          }
          #endif
          #ifdef _ENC_LIB_
          void cod::enc()
          {
          printf("cod::enc\n");
          }
          #endif

          //enc.h
          #ifndef enc_h
          #define enc_h
          class enc{
          void *penc;
          public:
          enc();
          ~enc();
          void encode();
          };
          #endif

          //dec.h
          #ifndef dec_h
          #define dec_h
          class dec{
          void *pdec;
          public:
          dec();
          ~dec();
          void decode();
          };
          #endif

          //codec.cpp
          #include "codec.h"
          #include "cod.h"

          #ifdef _DEC_LIB_
          dec::dec()
          {
          pdec = new cod;
          ((cod *)pdec)->read_file();
          }
          dec::~dec()
          {
          delete pdec;
          }
          void dec::decode()
          {
          ((cod *)pdec)->dec();
          }
          #endif
          #ifdef _ENC_LIB_
          enc::enc()
          {
          penc = new cod;
          ((cod *)penc)->read_file();
          }
          enc::~enc()
          {
          delete penc;
          }
          void enc::encode()
          {
          ((cod *)penc)->enc();
          }
          #endif

          //test.cpp
          #include "enc.h"
          #include "dec.h"
          #pragma comment(lib, "enc.lib")
          #pragma comment(lib, "dec.lib")
          void main(){
          enc e;
          e.encode();
          dec d;
          d.decode();
          }更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • I am very sorry I was wrong in #88377 and I am not able to resolve the problem. It seems there should be enc.h and dec.h in codec.cpp or it is included in the missing codec.h. Sorry again!
            • oh, sorry, in codec.cpp, #include "codec.h" should be changed to #include "enc.h" and #include "dec.h"
          • if this can be solved by some compiler/link setting, I just wonder why we still need namspace to solve name conflict.
            • namespace is not applicable to my case coz no name conflicting between the exported enc.h/dec.h. I do know how to solve it (just remove #ifdef/#endif in cod.cpp) but I am not clear about the reason.
              • sorry. should remove them in both cod.cpp and cod.h