2011-01-11 13 views
8

hello() फ़ंक्शन को किसी अन्य स्रोत (.cpp) फ़ाइल में स्थानांतरित करने या फ़ंक्शन का नाम बदलने के अलावा। क्या लिंकिंग त्रुटि से बचने के लिए कोई और तरीका है?एकाधिक परिभाषा लिंकिंग त्रुटि से कैसे बचें?

staticLibA.h

#ifndef _STATIC_LIBA_HEADER 
#define _STATIC_LIBA_HEADER 

int hello(void); 
int hello_staticLibA_only(void); 

#endif 

staticLibA.cpp

#include "staticLibA.h" 

int hello(void) 
{ 
    printf("\nI'm in staticLibA\n"); 
    return 0; 
} 

int hello_staticLibA_only(void) 
{ 
    printf("\nstaticLibA: hello_staticLibA_only\n"); 
    return 0; 
} 

उत्पादन:

g++ -c -Wall -fPIC -m32 -o staticLibA.o staticLibA.cpp 
ar -cvq ../libstaticLibA.a staticLibA.o 
a - staticLibA.o 

staticLibB.h

#ifndef _STATIC_LIBB_HEADER 
#define _STATIC_LIBB_HEADER 

int hello(void); 
int hello_staticLibB_only(void); 

#endif 

staticLibB.cpp

#include "staticLibB.h" 

int hello(void) 
{ 
    printf("\nI'm in staticLibB\n"); 
    return 0; 
} 

int hello_staticLibB_only(void) 
{ 
    printf("\nstaticLibB: hello_staticLibB_only\n"); 
    return 0; 
} 

उत्पादन:

g++ -c -Wall -fPIC -m32 -o staticLibB.o staticLibB.cpp 
ar -cvq ../libstaticLibB.a staticLibB.o 
a - staticLibB.o 

main.cpp

extern int hello(void); 
extern int hello_staticLibA_only(void); 
extern int hello_staticLibB_only(void); 

int main(void) 
{ 
    hello(); 
    hello_staticLibA_only(); 
    hello_staticLibB_only(); 
    return 0; 
} 

उत्पादन:

g++ -c -o main.o main.cpp 
g++ -o multipleLibsTest main.o -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt 
./libstaticLibB.a(staticLibB.o): In function `hello()': 
staticLibB.cpp:(.text+0x0): multiple definition of `hello()' 
./libstaticLibA.a(staticLibA.o):staticLibA.cpp:(.text+0x0): first defined here 
collect2: ld returned 1 exit status 
make: *** [multipleLibsTest] Error 1 

उत्तर

1

जोड़ने त्रुटि विशेष रूप से नमस्ते को दर्शाता है। यह दिखाता है क्योंकि दोनों पुस्तकालय "हैलो" की परिभाषा प्रदान करते हैं। यहां कोई अन्य लिंकिंग त्रुटि नहीं है।

आप या तो एक अलग पुस्तकालय में हैलो डाल सकते हैं, है नमस्ते एक अलग पुस्तकालय में रहते हैं, या सिर्फ एक हैलो वस्तु फ़ाइल [hello.o]

11

जब से तुम दोनों पुस्तकालयों ही दिखाई देते हैं के खिलाफ निष्पादन लिंक है, यह स्पष्ट नहीं है तुम क्यों समारोह का नाम नहीं बदल सकते हैं ...

अपने main में, आप इस लाइन है:

hello(); 

तुम यहाँ होने के लिए, यदि आप जोड़ने त्रुटि दूर जाने की उम्मीद कर रहे क्या? क्या इसे LibA, या LibB में कार्यान्वयन को कॉल करना चाहिए? यह निर्धारित करने के लिए कि आप कौन से फ़ंक्शन को लिंक करते हैं, यह निर्धारित करने के लिए कि आप लिंकर को पुस्तकालयों को पास करते हैं, बहुत बुरा विचार जैसा लगता है। एक वास्तविक उदाहरण में, क्या होगा यदि आपका hello_staticLibB_only फ़ंक्शन hello() पर कॉल कर रहा था? यह अन्य लाइब्रेरी में मौजूद फ़ंक्शन के संस्करण को कॉल कर सकता है ...

जैसा कि आप g++ का उपयोग कर रहे हैं, आपको अपने पुस्तकालय कार्यों को namespace में डालने पर विचार करना चाहिए (वे इस तरह से बचने में आपकी सहायता के लिए डिज़ाइन किए गए हैं संघर्ष का नाम)। यह आपके कोड और लिंकर दोनों विधियों के बीच अंतर बताने की अनुमति देगा।

LibA के लिए इस दृष्टिकोण के बाद, आप होगा:

staticLibA.h

#ifndef _STATIC_LIBA_HEADER 
#define _STATIC_LIBA_HEADER 

// Declare namespace to keep library functions together 
namespace LibA { 
    int hello(void); 
    int hello_staticLibA_only(void); 
} 

#endif 

staticLibA.cpp

#include "staticLibA.h" 
#include <stdio.h> 

// Indicate that contained function definitions belong in the LibA namespace 
namespace LibA { 
    int hello(void) 
    { 
     printf("\nI'm in staticLibA\n"); 
     return 0; 
    } 

    int hello_staticLibA_only(void) 
    { 
     printf("\nstaticLibA: hello_staticLibA_only\n"); 
     return 0; 
    } 
} 

main.cpp

// These declarations would usually be in a header... but I've left 
// them here to match your sample code... 

// declare relevant functions to belong to the LibA namespace 
namespace LibA{ 
    extern int hello(void); 
    extern int hello_staticLibA_only(void); 
} 

// declare relevant functions from LibB (note they are not 
// in a namespace) 
extern int hello(void); 
extern int hello_staticLibB_only(void); 

int main(void) 
{ 
    // Explicitly call the hello from LibA 
    LibA::hello(); 
    // Call the other library function (also from LibA) 
    LibA::hello_staticLibA_only(); 

    // Call library functions from LibB (note they don't require a namespace 
    // because I haven't updated it to have one) 
    hello(); 
    hello_staticLibB_only(); 
    return 0; 
} 
+0

+1: विषय पर कहने के लिए और कुछ नहीं। – rubenvb

संबंधित मुद्दे