From: don r. <don...@ya...> - 2008-04-17 21:05:13
|
I wrote a DLL and created a ".a" library (with MinGW/GCC). Then I tried to compile a new DLL that uses the functions and classes defined in that library. However, I got multiple errors: "Undefined reference to '.....'" The only way I got it to work was to include the object files instead of the library. Why is this? I thought I could compile it with just the lib file? 'm using ant to make it, so i apologize but I think the name of the tags should be fairly clear. MY CODE/COMMANDS -------------- <cc link="shared" outtype="shared" multithreaded="true" optimize="speed" objdir="." outfile="TryIt" > <compilerarg value="-Wall"/> <compilerarg value="-fno-strict-aliasing"/> <linker name="gcc"> <linkerarg value="--kill-at"/> <linkerarg value="-oTryIt.dll"/> <!-- If I include these, it compiles. If instead I include the library and NOT these object files, it cannot find the definitions. --> <linkerarg value="TestClass.o" /> <linkerarg value="Globals.o" /> </linker> <sysincludepath location="C:/Program Files/MinGW/Beta"/> <sysincludepath location="."/> <fileset dir="." includes="*.cpp"/> <libset libs="stdc++" /> </cc> And my CPP file includes the header with all the function/class declarations: #include <Test.h> (All the classes that are in the lib were also compiled with that same header included in them as well) (Below is basically what my header file is like) [CODE] #include <iostream> #include <vector> #include <sstream> #ifndef MYHEADER_ #define MYHEADER_ //All the basic functions are like this extern void changeType(std::string name, std::vector<std::string>& vec); //I have a few templated functions template <typename T> void changeType(T& type) { ...elided... } //I have a bunch of class declarations without the implementations class SomeClass { public: void doSomething(); }; #endif [/CODE] All the class functions were implemented in <class_name>.cpp files and the extern functions in functions.cpp. I then compiled it into one DLL and made a lib file from that (as well as the object files). ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Greg C. <gch...@sb...> - 2008-04-17 22:03:48
|
On 2008-04-17 21:05Z, don rhummy wrote: > I wrote a DLL and created a ".a" library (with > MinGW/GCC). Then I tried to compile a new DLL that > uses the functions and classes defined in that > library. However, I got multiple errors: > > "Undefined reference to '.....'" > > The only way I got it to work was to include the > object files instead of the library. Why is this? I > thought I could compile it with just the lib file? The linker command should specify the library that you want to link with, after any object files that might need the library. > 'm using ant to make it, so i apologize but I think > the name of the tags should be fairly clear. I can guess what some of it might mean, but it would be really helpful if you would show the actual linker command that's executed by your build system. > <linker name="gcc"> You're writing C++, so use 'g++' (not 'gcc') as the linker. |
From: don r. <don...@ya...> - 2008-04-17 22:49:22
|
For some reason g++ freezes when I use it with Ant. No idea why, so I use gcc instead. I was finally able to get it to work but only by linking to the DLL instead of the ".lib" file. For some reason the ".lib" file (and the ".a" file) are not adequate for defining the functions, etc. Why is that? How should I be making a lib so that it is complete and valid for linking? --- Greg Chicares <gch...@sb...> wrote: > On 2008-04-17 21:05Z, don rhummy wrote: > > I wrote a DLL and created a ".a" library (with > > MinGW/GCC). Then I tried to compile a new DLL that > > uses the functions and classes defined in that > > library. However, I got multiple errors: > > > > "Undefined reference to '.....'" > > > > The only way I got it to work was to include the > > object files instead of the library. Why is this? > I > > thought I could compile it with just the lib file? > > The linker command should specify the library that > you > want to link with, after any object files that might > need > the library. > > > 'm using ant to make it, so i apologize but I > think > > the name of the tags should be fairly clear. > > I can guess what some of it might mean, but it would > be > really helpful if you would show the actual linker > command > that's executed by your build system. > > > <linker name="gcc"> > > You're writing C++, so use 'g++' (not 'gcc') as the > linker. > > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 > JavaOne(SM) Conference > Don't miss this year's exciting event. There's still > time to save $100. > Use priority code J8TL2D2. > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > _______________________________________________ > MinGW-users mailing list > Min...@li... > > You may change your MinGW Account Options or > unsubscribe at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Giel v. S. <me...@mo...> - 2008-04-17 23:07:57
Attachments:
signature.asc
|
don rhummy schreef: > For some reason g++ freezes when I use it with Ant. No > idea why, so I use gcc instead. > > I was finally able to get it to work but only by > linking to the DLL instead of the ".lib" file. For > some reason the ".lib" file (and the ".a" file) are > not adequate for defining the functions, etc. Why is > that? How should I be making a lib so that it is > complete and valid for linking? For making static link libraries you should be using "ar". E.g. this will create a library from the given object files: $ ar rcv libstuff.a thing.o it.o this.o something.o stuff.o AFAIK you should use neither gcc, nor g++ for the creation of the static libraries as "ar" is just a generic archival utility that puts all the given object files in a single library archive. -- Giel |
From: Greg C. <gch...@sb...> - 2008-04-17 23:19:03
|
On 2008-04-17 22:49Z, don rhummy wrote: > > I was finally able to get it to work but only by > linking to the DLL instead of the ".lib" file. For > some reason the ".lib" file (and the ".a" file) are > not adequate for defining the functions, etc. Why is > that? How should I be making a lib so that it is > complete and valid for linking? What kind of library do you want it to be: static, or shared (dll)? If you want a static library, use 'ar'. If you want an import library to accompany your dll, then use '--out-implib' and specify its filename. Because you're using a compiler driver to link, use: -shared -Wl,--out-implib,libwhatever.a where '-shared' means create a dll '-Wl' means pass options to the linker 'whatever' is the base name of your library Then, when you compile a program that uses that dll, you can write '-lwhatever'. But you really don't have to do all of that. As you've already discovered, with gcc you can just link directly to the dll--see: http://sourceware.org/binutils/docs-2.18/ld/WIN32.html |
From: don r. <don...@ya...> - 2008-04-18 14:30:36
|
> What kind of library do you want it to be: static, > or > shared (dll)? I want it to be shared. Am I coding it properly? Do I have to put either of the following in the header file (some people have said I need to do this): 1. #ifdef _cplusplus extern "C" {.... 2. __declspec(export) I really would prefer not littering all my code with that. What about all the "extern" keywords I had to put on the function delcarations, I thought that was proper (and it didn't compile without that) but someone on another board says it's "wrong and worrisome." > If you want an import library to accompany your dll, > then use '--out-implib' and specify its filename. > Because you're using a compiler driver to link, use: > -shared -Wl,--out-implib,libwhatever.a > where > '-shared' means create a dll > '-Wl' means pass options to the linker > 'whatever' is the base name of your library > Then, when you compile a program that uses that dll, > you can write '-lwhatever'. OK, and that'll make it shared, right? Because now I believe it's linking statically since the new DLL is so huge yet it doesn't have that much code. Thank you for all your help! ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Greg C. <gch...@sb...> - 2008-04-19 00:07:29
|
On 2008-04-18 14:30Z, don rhummy wrote: >> What kind of library do you want it to be: static, >> or >> shared (dll)? > > I want it to be shared. Am I coding it properly? Yes, AFAICT. > Do I > have to put either of the following in the header file > (some people have said I need to do this): In this context, no, AFAICT. Here's why: > 1. #ifdef _cplusplus extern "C" {.... You'd do that in a header if you want to be able to use it with C as well as C++. But you seem to be using only C++, so '__cplusplus' is always defined. [Note that there are two leading underscores; the condition above is false in standard C++.] However, if you need to mix compilers: http://www.mingw.org/MinGWiki/index.php/MixingCompilers then you could use 'extern "C"' to add C-linkage functions that invoke code that has C++ linkage. > 2. __declspec(export) I think that has to be '__declspec(dllexport)'. ^^^ You probably don't need "decorations" like that as long as you're directly linking a gcc-compiled dll to a gcc- compiled application. (I say "probably" in case you try exporting data; yet these workarounds --enable-auto-import --enable-runtime-pseudo-relocs are available, as discussed in the binutils manual.) OTOH, the decorations are robust and portable: any msw compiler would support them, but not all compilers support direct dll linking. > I really would prefer not littering all my code with > that. What about all the "extern" keywords I had to > put on the function delcarations, I thought that was > proper (and it didn't compile without that) but > someone on another board says it's "wrong and > worrisome." Aside from the special case of 'extern "C"', 'extern' isn't as common in idiomatic C++ as in C, because data are probably encapsulated in classes--so I'd normally question it in code review. Right now I can't think of a C++ case where omitting 'extern' from a function declaration would produce a compile-time error. What was the error message? >> If you want an import library to accompany your dll, >> then use '--out-implib' and specify its filename. >> Because you're using a compiler driver to link, use: >> -shared -Wl,--out-implib,libwhatever.a >> where >> '-shared' means create a dll >> '-Wl' means pass options to the linker >> 'whatever' is the base name of your library >> Then, when you compile a program that uses that dll, >> you can write '-lwhatever'. > > OK, and that'll make it shared, right? Yes. > Because now I > believe it's linking statically since the new DLL is > so huge yet it doesn't have that much code. It might seem large for reasons mentioned here: http://www.mingw.org/MinGWiki/index.php/executables%20large but it's rarely worth twiddling options to make it smaller. |
From: don r. <don...@ya...> - 2008-04-19 00:24:14
|
Thank you so much for your help so far! > Right now I can't think of a C++ case where omitting > 'extern' from a function declaration would produce a > compile-time error. What was the error message? "undefined reference" and then the name of each of the global functions. My setup was like this: header.h --------- * class declarations * global function declarations TestClass.cpp ---------------- * #include header.h * has implementation of TestClass TestExceptionClass.cpp ------------------------ * #include header.h * has implementation of TestExceptionClass Functions.cpp --------------- * #include header.h * has implementation of every global function Then when I compiled it into a DLL with GCC/G++, the linker says "unreseolved reference..." for every one of the global functions that I used. The only thing that fixed the problem was putting extern in front of the function delcarations in header.h. Any idea what's causing this? (Or is this correct?) ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: don r. <don...@ya...> - 2008-04-18 16:57:18
|
I've created a DLL with MinGW that has both overloaded functions (thus making extern "C" impossible) as well as templated functions (which I believe also makes them impossible with extern "C") and classes. I followed all the instructions for creating a ".def" file and a lib file from MSVC's lib tool. However, MSVC throws errors when I try to link to this DLL/lib. I believe it might be impossible: http://www.nabble.com/MSVC%2B%2B-linking-to-MinGW-compiled-DLL-td15182357.html http://aegisknight.org/cppinterface.html http://rzr.online.fr/wiki.php?nm Is that so? Why does this not work? Can someone please give me some sample code (and compilation instructions) of a DLL that has two overloaded functions and a template function that can be used from an MSVC dll? I want to make my DLL so that people can use it with their windows programs without forcing them to use MinGW to compile their programs, but I want to use MinGW instead of MSVC. Please help! Thanks. ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Chris W. <ch...@qw...> - 2008-04-18 20:34:52
|
Hi Don, On Fri, 18 Apr 2008, don rhummy wrote: > I've created a DLL with MinGW that has both overloaded functions (thus > making extern "C" impossible) as well as templated functions (which I > believe also makes them impossible with extern "C") and classes. I > followed all the instructions for creating a ".def" file and a lib file > from MSVC's lib tool. However, MSVC throws errors when I try to link to > this DLL/lib. > > I believe it might be impossible: > > http://www.nabble.com/MSVC%2B%2B-linking-to-MinGW-compiled-DLL-td15182357.html > http://aegisknight.org/cppinterface.html > http://rzr.online.fr/wiki.php?nm > > Is that so? Why does this not work? Name mangling and ABIs are different between different C++ compilers. > Can someone please give me some sample code (and compilation > instructions) of a DLL that has two overloaded functions and a template > function that can be used from an MSVC dll? I don't believe it can be compiled with any C++ compiler except MSVC. > I want to make my DLL so that people can use it with their windows > programs without forcing them to use MinGW to compile their programs, > but I want to use MinGW instead of MSVC. Provide two versions of the DLL? One compiled with MinGW and one with MSVC. (You may need different MinGW DLLs for gcc3, gcc4-sjlj and gcc4-dwarf). Cheers, Chris. -- _____ __ _ \ __/ / ,__(_)_ | Chris Wilson <0000 at qwirx.com> - Cambs UK | / (_/ ,\/ _/ /_ \ | Security/C/C++/Java/Ruby/Perl/SQL Developer | \ _/_/_/_//_/___/ | We are GNU : free your mind & your software | |
From: don r. <don...@ya...> - 2008-04-18 21:20:20
|
> Name mangling and ABIs are different between > different C++ compilers. I apologize for my *newbieness* here but does that mean that you can't buy/use any 3rd party DLLs (that were written in c++) unless they're compiled with the same compiler? (Sorry, I'm coming from Java here) So how do companies/individuals create utility DLLs to be used by other products? ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Greg C. <gch...@sb...> - 2008-04-19 01:02:13
|
On 2008-04-18 16:43Z, don rhummy wrote: > I've created a DLL with MinGW that has both overloaded > functions (thus making extern "C" impossible) as well > as templated functions (which I believe also makes > them impossible with extern "C") and classes. I Yes, class members and templates can't have "C" linkage. > followed all the instructions for creating a ".def" > file and a lib file from MSVC's lib tool. However, > MSVC throws errors when I try to link to this DLL/lib. > > I believe it might be impossible: > > http://www.nabble.com/MSVC%2B%2B-linking-to-MinGW-compiled-DLL-td15182357.html > http://aegisknight.org/cppinterface.html > http://rzr.online.fr/wiki.php?nm > > Is that so? http://www.mingw.org/MinGWiki/index.php/MixingCompilers > Why does this not work? It can work if everything you export is 'extern "C"'. It can work if you use the "COM" technique at the aegisknight.org link. It can work if both compilers use the same ABI. Few do. But in the general case it can't work because compilers generally use different ABIs, as Chris pointed out. > Can someone please > give me some sample code (and compilation > instructions) of a DLL that has two overloaded > functions and a template function that can be used > from an MSVC dll? It's ultimately hopeless. Sometimes somebody'll try to work around the name mangling, and it may seem to work for a while, but then it breaks, and can't be fixed. That's why Tor says The name mangling schemes are different *on purpose* in the nabble.com thread you cited above. It's a deliberate compile-time error that protects you from nasty run-time errors. What you *can* do is declare and export extern "C" char* my_strcat(char* a, char const* b); and implement it internally using a C++ std::string, sort of like this casual sketch: sa = std::string(a); sb = std::string(b); sa += sb; a = sa.c_str(); // Wrong: have to make a copy. return a; |
From: don r. <don...@ya...> - 2008-04-19 05:43:51
|
> The name mangling schemes are different *on > purpose* > in the nabble.com thread you cited above. It's a > deliberate compile-time error that protects you from > nasty run-time errors. > > What you *can* do is declare and export > extern "C" char* my_strcat(char* a, char const* > b); > and implement it internally using a C++ std::string, > sort of like this casual sketch: > sa = std::string(a); > sb = std::string(b); > sa += sb; > a = sa.c_str(); // Wrong: have to make a copy. > return a; Thanks for the reply. I have a question then: What's the point of the standard library if using it means you're not compatible with all c++ libraries? And why do all these compilers have to name-mangle differently if they're all compiled to the same code in the end (since the runtime is the same for all of them - on one OS)? Why can't the name mangling be "fixed"? ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ |
From: Brian D. <br...@de...> - 2008-04-19 05:54:37
|
don rhummy wrote: > What's the point of the standard library if using it > means you're not compatible with all c++ libraries? The only thing that is standard about C++ libraries is their source-level API interface. The underlying details about how they are implemented depends on the compiler. > And why do all these compilers have to name-mangle > differently if they're all compiled to the same code > in the end (since the runtime is the same for all of > them - on one OS)? Why can't the name mangling be "fixed"? That's a very naive view of how things work. Things are not "all the same in the end" because C++ leaves a great deal of under-the-hood details to be implemented by the compiler, and thus two compilers could generate completely different object code for the same source code. Name mangling prevents trying to put a metric nut on an inch thread would would completely strip and ruin both in the process. Brian |