Home » Language IDEs » C / C++ IDE (CDT) » Linker problem: undefined reference, trying to use a function defined in a .dll
Linker problem: undefined reference, trying to use a function defined in a .dll [message #155984] |
Mon, 10 October 2005 10:33  |
Eclipse User |
|
|
|
Originally posted by: corno.alum.mit.edu
Hello,
I'm starting with C++ again after a hiatus of 8 years. I'm running Eclipse
+ CDT + cygwin on Windows XP. I have created a DLL containing,
appropriately, a class called Hello which defines a public sayHello
function.
void sayHello()
{
cout << "Hello World" << endl;
}
This DLL is called HelloLib.dll and resides in P:/eclipse/HelloLib/Debug.
I then try to use Hello::sayHello() in another project, like so:
int main (int argc, char** argv)
{
Hello hello;
hello.sayHello();
return 0;
}
This will compile, but it will not link. The errors I get are as follows:
Building target: Sleepycat.exe
Invoking: GCC C++ Linker
g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o -lHelloLib
/main.o: In function `main':
/cygdrive/p/eclipse/Sleepycat/Debug/../main.cpp:10: undefined reference to
`Hello::sayHello()'
collect2: ld returned 1 exit status
make: *** [Sleepycat.exe] Error 1
What am I missing here? I searched for all occurrences of "undefined
reference" in the newsgroup archive, but was unable to find anything
specific to using DLLs (do these work differently to .so files on Unix?)
Many thanks,
Petra
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #155992 is a reply to message #155984] |
Tue, 11 October 2005 03:18   |
Eclipse User |
|
|
|
Hi!
When you use the -l flag for the linker your libraries should follow the
name
convention: "lib(YOURLIBNAME).a", or "lib(YOURLIBNAME).so" for shared
libraries.
Your lib name is missing the first "lib" and you are using the "dll"
filetype which is only
used by windows compilers (Visual Studio, Intel,...).
Try the following solutions:
1) Rename your lib to libsayHello.so and try again
or 2) Change your linker flag to:
g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o HelloLib.so
In both cases, you shouldn't use the dll ending if you use the
g++ compiler!
I hope this helps
andrés
Am Mon, 10 Oct 2005 16:33:10 +0200 schrieb Petra Chong
<corno@alum.mit.edu>:
> Hello,
>
> I'm starting with C++ again after a hiatus of 8 years. I'm running
> Eclipse + CDT + cygwin on Windows XP. I have created a DLL containing,
> appropriately, a class called Hello which defines a public sayHello
> function.
> void sayHello()
> {
> cout << "Hello World" << endl;
> }
>
> This DLL is called HelloLib.dll and resides in P:/eclipse/HelloLib/Debug.
>
> I then try to use Hello::sayHello() in another project, like so:
>
> int main (int argc, char** argv)
> {
> Hello hello;
> hello.sayHello();
>
> return 0;
> }
>
> This will compile, but it will not link. The errors I get are as follows:
>
> Building target: Sleepycat.exe
> Invoking: GCC C++ Linker
> g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o -lHelloLib
> /main.o: In function `main':
> /cygdrive/p/eclipse/Sleepycat/Debug/../main.cpp:10: undefined reference
> to `Hello::sayHello()'
> collect2: ld returned 1 exit status
> make: *** [Sleepycat.exe] Error 1
>
> What am I missing here? I searched for all occurrences of "undefined
> reference" in the newsgroup archive, but was unable to find anything
> specific to using DLLs (do these work differently to .so files on Unix?)
>
> Many thanks,
>
> Petra
>
--
Written with Opera
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #155997 is a reply to message #155992] |
Tue, 11 October 2005 04:15   |
Eclipse User |
|
|
|
Originally posted by: corno.alum.mit.edu
Andrés wrote:
> Hi!
> When you use the -l flag for the linker your libraries should follow the
> name
> convention: "lib(YOURLIBNAME).a", or "lib(YOURLIBNAME).so" for shared
> libraries.
> Your lib name is missing the first "lib" and you are using the "dll"
> filetype which is only
> used by windows compilers (Visual Studio, Intel,...).
> Try the following solutions:
> 1) Rename your lib to libsayHello.so and try again
> or 2) Change your linker flag to:
> g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o HelloLib.so
> In both cases, you shouldn't use the dll ending if you use the
> g++ compiler!
> I hope this helps
> andrés
Hi Andrés,
I'm afraid it did not work...
Here is the full transcript of what I have tried.
1. Build library:
make -k clean all
rm -rf ./Hello.o ./Hello.d HelloLib.dll
Building file: ../Hello.cpp
Invoking: GCC C++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -oHello.o ../Hello.cpp
Finished building: ../Hello.cpp
Building target: HelloLib.dll
Invoking: GCC C++ Linker
g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
-Wl,--output-def=libHello.def -oHelloLib.dll ./Hello.o
Creating library file: libHello.a
Finished building target: HelloLib.dll
Build complete for project HelloLib
2. Build the application that is trying to link to HelloLib (I did not yet
rename HelloLib.dll to libHello.so)
make -k clean all
rm -rf ./main.o ./main.d HelloWorld.exe
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -IP:/eclipse/HelloLib -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
../main.cpp
Finished building: ../main.cpp
Building target: HelloWorld.exe
Invoking: GCC C++ Linker
g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHello
/main.o: In function `main':
/cygdrive/p/eclipse/HelloWorld/Debug/../main.cpp:10: undefined reference
to `Hello::sayHello()'
collect2: ld returned 1 exit status
make: *** [HelloWorld.exe] Error 1
make: Target `all' not remade because of errors.
Build complete for project HelloWorld
3. Rename HelloLib.dll to libHello.so, tried the above, got EXACTLY the
same output.
4. Tried the command line you suggested (with appropriate changes in path)
$ g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o libHello.so
g++: libHello.so: No such file or directory
I can confirm that libHello.so is in p:/eclipse/HelloLib/Release.
Couple of questions.
a) Is it possible just to rename a .dll to a .so without rebuilding? Seems
that I am misunderstanding something here?
b) If not, what do I need to do to build to a particular library type?
What is the difference between a .dll and a .so? What do all the compiler
flags do that set something to build as a shared library?
Thanks,
Petra
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #156005 is a reply to message #155997] |
Tue, 11 October 2005 06:00   |
Eclipse User |
|
|
|
There are still some strange configuration. You wrote:
> Invoking: GCC C++ Linker
> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
> -Wl,--output-def=libHello.def -oHelloLib.dll ./Hello.o
> Creating library file: libHello.a
> Finished building target: HelloLib.dll
Try to change the second line to:
> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
> -Wl,--output-def=libHello.def -olibHelloLib.so ./Hello.o
^^^^^^^^^^^^^^^^
and then compile the second project with
> g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHelloLib
^^^
andrés
Am Tue, 11 Oct 2005 10:15:42 +0200 schrieb Petra Chong
<corno@alum.mit.edu>:
> Andrés wrote:
>
>> Hi!
>
>> When you use the -l flag for the linker your libraries should follow
>> the name
>> convention: "lib(YOURLIBNAME).a", or "lib(YOURLIBNAME).so" for shared
>> libraries.
>> Your lib name is missing the first "lib" and you are using the "dll"
>> filetype which is only
>> used by windows compilers (Visual Studio, Intel,...).
>
>> Try the following solutions:
>> 1) Rename your lib to libsayHello.so and try again
>> or 2) Change your linker flag to:
>> g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o HelloLib.so
>
>> In both cases, you shouldn't use the dll ending if you use the
>> g++ compiler!
>> I hope this helps
>
>> andrés
>
> Hi Andrés,
>
> I'm afraid it did not work... Here is the full transcript of what I have
> tried.
>
> 1. Build library:
>
> make -k clean all rm -rf ./Hello.o ./Hello.d HelloLib.dll Building
> file: ../Hello.cpp
> Invoking: GCC C++ Compiler
> g++ -O3 -Wall -c -fmessage-length=0 -oHello.o ../Hello.cpp
> Finished building: ../Hello.cpp
> Building target: HelloLib.dll
> Invoking: GCC C++ Linker
> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
> -Wl,--output-def=libHello.def -oHelloLib.dll ./Hello.o
> Creating library file: libHello.a
> Finished building target: HelloLib.dll
> Build complete for project HelloLib
>
> 2. Build the application that is trying to link to HelloLib (I did not
> yet rename HelloLib.dll to libHello.so)
>
> make -k clean all rm -rf ./main.o ./main.d HelloWorld.exe
> Building file: ../main.cpp
> Invoking: GCC C++ Compiler
> g++ -IP:/eclipse/HelloLib -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
> ./main.cpp
> Finished building: ../main.cpp
> Building target: HelloWorld.exe
> Invoking: GCC C++ Linker
> g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHello
> /main.o: In function `main':
> /cygdrive/p/eclipse/HelloWorld/Debug/../main.cpp:10: undefined reference
> to `Hello::sayHello()'
> collect2: ld returned 1 exit status
> make: *** [HelloWorld.exe] Error 1
> make: Target `all' not remade because of errors.
> Build complete for project HelloWorld
>
> 3. Rename HelloLib.dll to libHello.so, tried the above, got EXACTLY the
> same output. 4. Tried the command line you suggested (with appropriate
> changes in path)
>
>
> $ g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o libHello.so
> g++: libHello.so: No such file or directory
>
> I can confirm that libHello.so is in p:/eclipse/HelloLib/Release.
>
>
>
> Couple of questions.
>
> a) Is it possible just to rename a .dll to a .so without rebuilding?
> Seems that I am misunderstanding something here?
> b) If not, what do I need to do to build to a particular library type?
> What is the difference between a .dll and a .so? What do all the
> compiler flags do that set something to build as a shared library?
>
> Thanks,
>
> Petra
>
--
Written with Opera
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #156013 is a reply to message #156005] |
Tue, 11 October 2005 08:12   |
Eclipse User |
|
|
|
Originally posted by: corno.alum.mit.edu
Andrés wrote:
> There are still some strange configuration. You wrote:
>> Invoking: GCC C++ Linker
>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>> -Wl,--output-def=libHello.def -oHelloLib.dll ./Hello.o
>> Creating library file: libHello.a
>> Finished building target: HelloLib.dll
> Try to change the second line to:
>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>> -Wl,--output-def=libHello.def -olibHelloLib.so ./Hello.o
> ^^^^^^^^^^^^^^^^
> and then compile the second project with
>> g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHelloLib
> ^^^
> andrés
OK, tried again... got this:
1. Building the library:
Building file: ../Hello.cpp
Invoking: GCC C++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -oHello.o ../Hello.cpp
Finished building: ../Hello.cpp
Building target: libHelloLib.so
Invoking: GCC C++ Linker
g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
-Wl,--output-def=libHello.def -olibHelloLib.so ./Hello.o
Creating library file: libHello.a
Finished building target: libHelloLib.so
2. Importing / linking:
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -IP:/eclipse/HelloLib -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
../main.cpp
Finished building: ../main.cpp
Building target: HelloWorld.exe
Invoking: GCC C++ Linker
g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHelloLib
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin /bin/ld:
cannot find -lHelloLib
collect2: ld returned 1 exit status
make: *** [HelloWorld.exe] Error 1
make: Target `all' not remade because of errors.
Build complete for project HelloWorld
Still no joy, I am afraid. I had also tried, on someone else's suggestion,
to use this command line:
$ ls -la p:/eclipse/HelloLib/Release/*.a
-rw-r--r-- 1 chongpe mkgroup-l-d 6236 Oct 11 13:05
p:/eclipse/HelloLib/Release/libHello.a
chongpe@wldn0135446 ~/eclipse/HelloWorld/Debug
$ g++ -static -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o
-lHello
/main.o: In function `main':
/cygdrive/p/eclipse/HelloWorld/Debug/../main.cpp:10: undefined reference
to `Hello::sayHello()'
collect2: ld returned 1 exit status
where basically I try to link statically.
Thanks,
Petra
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #156020 is a reply to message #156013] |
Tue, 11 October 2005 10:01   |
Eclipse User |
|
|
|
ok, the shared version doesn't work because you are generating a
libHello.a file, not
a libHello.so.
aaaaaah, I think I found the error after rereading your first mail. your
method definition is
incomplete. You wrote
> void sayHello()
> {
> cout << "Hello World" << endl;
> }
but you should write
> void Hello::sayHello()
> {
> cout << "Hello World" << endl;
> }
you forgot to put the scope operator of the class!!!
And you should change your first linking line to:
>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>> -Wl,--output-def=libHello.def -oHello.dll ./Hello.o
and in the second linking process the -lHello flag.
Does it work now??
Am Tue, 11 Oct 2005 14:12:41 +0200 schrieb Petra Chong
<corno@alum.mit.edu>:
> Andrés wrote:
>
>> There are still some strange configuration. You wrote:
>
>>> Invoking: GCC C++ Linker
>>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>>> -Wl,--output-def=libHello.def -oHelloLib.dll ./Hello.o
>>> Creating library file: libHello.a
>>> Finished building target: HelloLib.dll
>
>> Try to change the second line to:
>>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>>> -Wl,--output-def=libHello.def -olibHelloLib.so ./Hello.o
>
>> and then compile the second project with
>>> g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHelloLib
>
>> andrés
>
>
> OK, tried again... got this:
>
> 1. Building the library:
>
> Building file: ../Hello.cpp
> Invoking: GCC C++ Compiler
> g++ -O3 -Wall -c -fmessage-length=0 -oHello.o ../Hello.cpp
> Finished building: ../Hello.cpp
> Building target: libHelloLib.so
> Invoking: GCC C++ Linker
> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
> -Wl,--output-def=libHello.def -olibHelloLib.so ./Hello.o
> Creating library file: libHello.a
> Finished building target: libHelloLib.so
>
>
> 2. Importing / linking:
>
> Building file: ../main.cpp
> Invoking: GCC C++ Compiler
> g++ -IP:/eclipse/HelloLib -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
> ./main.cpp
> Finished building: ../main.cpp
> Building target: HelloWorld.exe
> Invoking: GCC C++ Linker
> g++ -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHelloLib
> /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin /bin/ld:
> cannot find -lHelloLib
> collect2: ld returned 1 exit status
> make: *** [HelloWorld.exe] Error 1
> make: Target `all' not remade because of errors.
> Build complete for project HelloWorld
>
>
> Still no joy, I am afraid. I had also tried, on someone else's
> suggestion, to use this command line:
>
> $ ls -la p:/eclipse/HelloLib/Release/*.a
> -rw-r--r-- 1 chongpe mkgroup-l-d 6236 Oct 11 13:05
> p:/eclipse/HelloLib/Release/libHello.a
>
> chongpe@wldn0135446 ~/eclipse/HelloWorld/Debug
> $ g++ -static -LP:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o
> -lHello
> /main.o: In function `main':
> /cygdrive/p/eclipse/HelloWorld/Debug/../main.cpp:10: undefined reference
> to `Hello::sayHello()'
> collect2: ld returned 1 exit status
>
> where basically I try to link statically. Thanks,
>
> Petra
>
>
>
--
Written with Opera
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #156028 is a reply to message #156020] |
Tue, 11 October 2005 10:44   |
Eclipse User |
|
|
|
Originally posted by: corno.alum.mit.edu
Andrés wrote:
> ok, the shared version doesn't work because you are generating a
> libHello.a file, not
> a libHello.so.
> aaaaaah, I think I found the error after rereading your first mail. your
> method definition is
> incomplete. You wrote
>> void sayHello()
>> {
>> cout << "Hello World" << endl;
>> }
> but you should write
>> void Hello::sayHello()
>> {
>> cout << "Hello World" << endl;
>> }
> you forgot to put the scope operator of the class!!!
> And you should change your first linking line to:
>>> g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
>>> -Wl,--output-def=libHello.def -oHello.dll ./Hello.o
> and in the second linking process the -lHello flag.
> Does it work now??
No joy, I am afraid.
Here is what I did:
1. As you said, resolve the function properly:
void Hello::sayHello()
{
cout << "Hello World" << endl;
}
2. Build the library:
Building file: ../Hello.cpp
Invoking: GCC C++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -oHello.o ../Hello.cpp
Finished building: ../Hello.cpp
Building target: libHello.so
Invoking: GCC C++ Linker
g++ -shared -Wl,-soname=libHello.so -Wl,--out-implib=libHello.a
-Wl,--output-def=libHello.def -olibHello.so ./Hello.o
Creating library file: libHello.a
Finished building target: libHello.so
(I changed it back to do libHello.so)
3. Try to link it:
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -IP:/eclipse/HelloLib -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
../main.cpp
Finished building: ../main.cpp
Building target: HelloWorld.exe
Invoking: GCC C++ Linker
g++ -Lp:/eclipse/HelloLib/Release -oHelloWorld.exe ./main.o -lHello
/main.o: In function `main':
/cygdrive/p/eclipse/HelloWorld/Debug/../main.cpp:10: undefined reference
to `Hello::sayHello()'
collect2: ld returned 1 exit status
make: *** [HelloWorld.exe] Error 1
make: Target `all' not remade because of errors.
Build complete for project HelloWorld
It's still the same error as the original one. If I rename the library to
Hello.dll or anything else other than libHello.so it seems to not even
find it.
I'm really at a dead end, I am using Visual C++ at the moment because I
cannot get Eclipse working. I can't tell if it's because I don't know how
to use g++; i have looked at a lot of man pages, but can't seem to find
anything that suggests I should be doing anything different from what I am
doing now.
Thanks for your help Andrés.
Petra
|
|
|
Re: Linker problem: undefined reference, trying to use a function defined in a .dll [message #156434 is a reply to message #155984] |
Mon, 17 October 2005 12:55  |
Eclipse User |
|
|
|
Originally posted by: mendoza.anafocus.com
Hi Petra.
I tried to read the evolution of your problem but I got a little bit lost,
so I will start again.
I've just solved a similar problem, this is, compile against a DLL using
g++.
Basically, you have to create a static library "libXXXX.a" from the DLL so
the g++ understands it.
This are the steps I've taken: (substitute "library" for your dll file
name)
1. Execute the following lines:
echo EXPORTS library.def
pexports library.dll | sed 's/^_//' > library.def
This creates a .def file that contents all the DLL functions definitions.
2. Now create the liblibrary.a file executing:
dlltool --input-def library.def --dllname library.dll --output-lib
liblibrary.a -k
3. Now, to be able to use liblibrary.a, execute:
cp liblibrary.a /usr/lib/w32api
ranlib /usr/lib/w32api/liblibrary.a
4. To link against the liblibrary.a file, use the "-llibrary" option.
5. If you still get an "undefined reference to some function" message like:
"undefined reference to SomeFunction"
go to the library.def file, rename that function to have that exact name
and repeat from step 2.
Hope this helps.
Carlos
Petra Chong wrote:
> Hello,
> I'm starting with C++ again after a hiatus of 8 years. I'm running Eclipse
> + CDT + cygwin on Windows XP. I have created a DLL containing,
> appropriately, a class called Hello which defines a public sayHello
> function.
> void sayHello()
> {
> cout << "Hello World" << endl;
> }
> This DLL is called HelloLib.dll and resides in P:/eclipse/HelloLib/Debug.
> I then try to use Hello::sayHello() in another project, like so:
> int main (int argc, char** argv)
> {
> Hello hello;
> hello.sayHello();
> return 0;
> }
> This will compile, but it will not link. The errors I get are as follows:
> Building target: Sleepycat.exe
> Invoking: GCC C++ Linker
> g++ -LP:/eclipse/HelloLib/Debug -oSleepycat.exe ./main.o -lHelloLib
> /main.o: In function `main':
> /cygdrive/p/eclipse/Sleepycat/Debug/../main.cpp:10: undefined reference to
> `Hello::sayHello()'
> collect2: ld returned 1 exit status
> make: *** [Sleepycat.exe] Error 1
> What am I missing here? I searched for all occurrences of "undefined
> reference" in the newsgroup archive, but was unable to find anything
> specific to using DLLs (do these work differently to .so files on Unix?)
> Many thanks,
> Petra
|
|
|
Goto Forum:
Current Time: Thu May 01 20:58:32 EDT 2025
Powered by FUDForum. Page generated in 0.04586 seconds
|