2010-11-29 15 views
6

मैं लिनक्स में हूं। मेरे Makefile फ़ाइल इसमैं डायनामिक लिंक लाइब्रेरी को सही तरीके से कैसे लिंक और बनाया?

main2: main.cpp 
g++ -c $(LIBS) $(CFLAGS) -fPIC main.cpp 
g++ -shared main.o -o main.so 

कहाँ,

SDL_CFLAGS := $(shell sdl-config --cflags) 
SDL_LDFLAGS := $(shell sdl-config --libs) 
CC = gcc 
COPTS = -g -Wall 
CFLAGS = $(SDL_CFLAGS) 
LIBS = -lstdc++ -lSDL $(SDL_LDFLAGS) -L/usr/X11R6/lib -lGL -lGLU 

जो

g++ -c -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -fPIC main.cpp 

g++ -shared main.o -o main.so 

अब चलाता है इस त्रुटि के बिना काम करता है। main.o और main.so फाइलें बनाई जाती हैं।

हालांकि, जब मैं अजगर ctypes

from ctypes import * import os 
libtest = cdll.LoadLibrary(os.getcwd()+ '/main.so') libtest.main_loop() 
libtest.main_loop() 

साथ main.os लिंक करने का प्रयास मैं यह त्रुटि

>>> libtest = cdll.LoadLibrary(os.getcwd() + '/main.so') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.6/ctypes/__init__.py", line 431, in LoadLibrary 
    return self._dlltype(name) 
    File "/usr/lib/python2.6/ctypes/__init__.py", line 353, in __init__ 
    self._handle = _dlopen(self._name, mode) 
OSError: /home/atomos/DF/open_gl_client/ctypes_client/main.so: undefined symbol: glEnd 

मुझे यकीन है कि अगर मैं जुड़ा हुआ पुस्तकालय सही ढंग से बनाने व्यक्ति हूं मिलता है। मैं एक लिंक्ड लाइब्रेरी कैसे बना सकता हूं जिसे मैं लोड कर सकता हूं?

क्या मुझे main.cpp से आयात की जाने वाली प्रत्येक लाइब्रेरी के लिए .o और .os फ़ाइल बनाना है या क्या यह स्वचालित रूप से देखभाल की जाती है?

मुझे समझ में नहीं आता कि कंपाइलर या लिंकर क्या कर रहा है, हालांकि यह किसी भी आयात के साथ एक साधारण उदाहरण के लिए काम करता है, लेकिन सीपीपी फाइलों के लिए जो ओपनजीएल पुस्तकालयों को आयात करता है, यह त्रुटि देता है।

---- अद्यतन ---- main.so पैदावार के खिलाफ ldd

ldd main.so 
     linux-gate.so.1 => (0xb7755000) 
     libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xaf625000) 
     libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xaf5ff000) 
     libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xaf4a4000) 
     libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xaf485000) 
     /lib/ld-linux.so.2 (0xb7756000) 

---- अद्यतन ----

मैं भाग गया जी ++ दूसरे में कमरा साझा ध्वज के बिना संकलन कदम और प्राप्त इस त्रुटि

g++ main.o -o main.so 
main.o: In function `Texture_map_list::add_texture(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': 
main.cpp:(.text+0xf0fe): undefined reference to `glGenTextures' 
main.cpp:(.text+0xf2ba): undefined reference to `glBindTexture' 
main.cpp:(.text+0xf2d7): undefined reference to `glTexParameterf' 
main.cpp:(.text+0xf2f4): undefined reference to `glTexParameterf' 
main.cpp:(.text+0xf310): undefined reference to `glTexParameteri' 
main.cpp:(.text+0xf32c): undefined reference to `glTexParameteri' 
main.cpp:(.text+0xf365): undefined reference to `gluBuild2DMipmaps' 
main.o: In function `init()': 
main.cpp:(.text+0xf457): undefined reference to `SDL_Init' 
main.cpp:(.text+0xf46d): undefined reference to `SDL_WM_SetCaption' 
main.cpp:(.text+0xf472): undefined reference to `SDL_GetVideoInfo' 
main.cpp:(.text+0xf480): undefined reference to `SDL_GetError' 
main.cpp:(.text+0xf497): undefined reference to `SDL_Quit' 
main.cpp:(.text+0xf4e2): undefined reference to `SDL_GL_SetAttribute' 
main.cpp:(.text+0xf505): undefined reference to `SDL_SetVideoMode' 
main.cpp:(.text+0xf513): undefined reference to `SDL_GetError' 
main.cpp:(.text+0xf52a): undefined reference to `SDL_Quit' 
main.cpp:(.text+0xf559): undefined reference to `glClearColor' 
main.cpp:(.text+0xf565): undefined reference to `glEnable' 
main.cpp:(.text+0xf571): undefined reference to `glMatrixMode' 
main.cpp:(.text+0xf576): undefined reference to `glLoadIdentity' 
main.cpp:(.text+0xf5a2): undefined reference to `gluPerspective' 
main.o: In function `process_keypresses()': 
main.cpp:(.text+0x10678): undefined reference to `SDL_PollEvent' 
main.cpp:(.text+0x109a1): undefined reference to `SDL_PollEvent' 
main.o: In function `main_loop': 
main.cpp:(.text+0x10d76): undefined reference to `SDL_GetKeyState' 
main.cpp:(.text+0x10d9f): undefined reference to `SDL_Quit' 
main.o: In function `render()': 
main.cpp:(.text+0x10e00): undefined reference to `SDL_GetMouseState' 
main.cpp:(.text+0x10e90): undefined reference to `SDL_GetMouseState' 
main.cpp:(.text+0x10f94): undefined reference to `glClear' 
main.cpp:(.text+0x10fa0): undefined reference to `glMatrixMode' 
main.cpp:(.text+0x10fa5): undefined reference to `glLoadIdentity' 
main.cpp:(.text+0x11081): undefined reference to `gluLookAt' 
main.cpp:(.text+0x11120): undefined reference to `glTranslatef' 
main.cpp:(.text+0x1114d): undefined reference to `glRotatef' 
main.cpp:(.text+0x1117a): undefined reference to `glRotatef' 
main.cpp:(.text+0x1119e): undefined reference to `SDL_GL_SwapBuffers' 
main.o: In function `draw_plane(float, float, float)': 
main.cpp:(.text+0x111d8): undefined reference to `glColor3f' 
main.cpp:(.text+0x111e4): undefined reference to `glBegin' 
main.cpp:(.text+0x1120b): undefined reference to `glVertex3f' 

....

+0

"फाइल बनाएं" - मुझे लगता है कि आपका मतलब है * मेकफ़ाइल *। –

उत्तर

11

सबसे पहले, ध्यान दें कि थी यदि आप साझा लाइब्रेरी नहीं बनाते हैं तो डीबग करना आसान है। आप शेर के साथ निर्माण कर रहे हैं ताकि लिंकर त्रुटियां तब तक न हों जब तक आप पाइथन में लाइब्रेरी को गतिशील रूप से लोड नहीं करते। कमरा साझा करते समय आप डीबग कर रहे हैं बंद कर दें, और जब आप लिंक करने की कोशिश आप कमांड लाइन पर त्रुटियों देखेंगे:

g++ main.o -o main 
main.o: In function `main': 
main.cpp:(.text+0x1d): undefined reference to `glBegin' 
main.cpp:(.text+0x22): undefined reference to `glEnd' 
collect2: ld returned 1 exit status 

अब समस्या यह है कि आप संकलक करने के लिए लिंकर तर्क गुजर रहे है। मैं देखता हूं कि आपने मेकफ़ाइल में CFLAGS और LIBS को अच्छी तरह से अलग किया है। अच्छा। लेकिन आप main.o. बनाने के लिए पहली पंक्ति पर $(LIBS) और $(CFLAGS) दोनों को पारित कर रहे हैं। उस लाइन पर LIBS को अनदेखा कर दिया जाएगा, क्योंकि वे लिंक झंडे हैं।

दूसरी पंक्ति पर, जहां आप वास्तव में अंतिम निष्पादन योग्य बना रहे हैं, तो आप $(LIBS) पास नहीं करते हैं, इसलिए प्रोग्राम libGL या किसी अन्य पुस्तकालय से जुड़ा नहीं है।

इसलिए, बस अपने makefile thusly तय:

main2: main.cpp 
    g++ -c $(CFLAGS) -fPIC main.cpp 
    g++ -shared main.o $(LIBS) -o main.so 

संपादित: मैं के बाद से एहसास हो गया है कि जीसीसी में, आप always put the libraries after the object files चाहिए, तो मैं तदनुसार makefile की अंतिम पंक्ति बदल दिया है।

+0

इस समस्या को हल किया गया। धन्यवाद। – HaltingState

+5

सुनने के लिए बहुत अच्छा है। क्या आप हल किए गए इसे ध्वजांकित करने के लिए 'स्वीकृत उत्तर' चेकबॉक्स पर टिक टिक सकते हैं? धन्यवाद। – mgiuca

+0

मैंने स्वीकार किए गए उत्तर बॉक्स को हिट किया और ऊपर उठाया। क्षमा करें, कि मैं ऐसा करना भूल गया। – HaltingState

1

सरल:

आप लिंक झंडे से गुजर रहे हैं संकलन कदम पर आपत्ति करने के लिए।

यहाँ आप क्या जरूरत है:

g++ -c -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT main.cpp 
g++ -lstdc++ -lSDL -L/usr/lib -lSDL -L/usr/X11R6/lib -lGL -lGLU -fPIC main.o -o main 

प्लस हो सकता है आप को बनाने के लिए फ़ाइल है, लेकिन एक सामान्य निष्पादन करने के लिए नहीं करना चाहती।

+0

ठीक है, वह एक ऐसी फाइल बनाना चाहता है, क्योंकि वह इसे पायथन में आयात कर रहा है। – mgiuca

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