But I can't compile to to an executable using g++, the linker complains about 'undefined references'. I have googled the problem and found several solutions dealing with lib files, but I'm not using any libs, just a source file containing the clases, etc, an example source, and a header file. I'm trying to compile the example.
I'm on Windows using MinGW. I've also tried compiling from my IDE, Eclipse, but it produced the error:
/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'
I just tested it in VC++ 5.0 and similar errors occur.
This happens with several other 3rd party sources I've tried to compile. I've tried 4 different sources and all of them complain about undefined references while linking.
The only thing that's different about them than what I've been compiling before, where I had no problems, is classes. They all use classes, and I don't think I've ever tried to compile anything with classes before.
I haven't looked at the code, but I'd guess that you need to use a Microsoft compiler with and tell it you're building a console app. The IDE will define _CONSOLE (rather than _WINDOWS).
@kbw
I tried using MSVC++ 5.0 on all of them. Mainly, I got this:
error LNK2001: unresolved external symbol __chkesp
But some sources just spit out tons of errors too fast to read...
One source was specifically a VC6 project, and others were generic ANSI code.
@Bazzy
I have just been using g++ command, as well as building via IDE's, like MSVC and Eclipse (Eclipse uses g++).
I did not try using the makefile... How do I use it? I don't have a make command. Also, that one I linked to is the only source out of what I've tried, that even has a makefile.
I'm sure I would have no problems if they didn't use classes... I think I remember trying to compile somebody's source from these forums, who was using classes, and if I remember correctly, it produced similar errors, though at the time I just thought he didn't code it right.
C:\C++ Projects\ConfigFile>make
'make' is not recognized as an internal or external command,
operable program or batch file.
I mentioned I was using MinGW, but you're right, I thought it came with make.
I guess I can download it. Know a good place to get it?
Nevermind, I would need to install it using the MinGW package manager, since I'm on windows.
As far as I can tell, the package manager did download and install make, but I will try to reinstall it... It also tells me that I haven't installed the Objective C compiler, and I probably haven't, so I'll install that too.
I found the problem. The executable is named mingw32-make.exe not make.
Upon running make, I receive the following error:
C:\C++ Projects\ConfigFile>mingw32-make.exe
g++ -Wall -ansi -o example example.cpp ConfigFile.cpp
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/postypes.h:46,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iosfwd:50,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algobase.h:70,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/char_traits.h:46,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/string:47,
from example.cpp:4:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:161: error: `::swprintf' has not been de
clared
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:168: error: `::vswprintf' has not been d
eclared
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/postypes.h:46,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/iosfwd:50,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algobase.h:70,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/char_traits.h:46,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/string:47,
from ConfigFile.h:46,
from ConfigFile.cpp:3:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:161: error: `::swprintf' has not been de
clared
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/cwchar:168: error: `::vswprintf' has not been d
eclared
mingw32-make.exe: *** [example] Error 1
It worked! I don't quite get it though... Why did it work? Is it because of ConfigFile.cpp specified before example.cpp? Does that tell the linker that the references are within ConfigFile.cpp?
How does it work? I never read about that in the man pages, --help output, anything.
I can't believe how simple it was. I'm more surprised I never learned about something so fundamental...
Thank you very much helios! You've no idea how much that helps me.
The order in which you pass filenames to GCC is irrelevant. You were just linking things the wrong way.
First you were trying to compile ConfigFile.cpp as a complete program, which it isn't. That's why you were getting an error about main() being undefined.
Then you tried to compile using VC++ 5.0. That's a mistake in itself, since that compiler is over ten years old.
As for when you tried to make, I'm not sure, but I've heard from different sources that mingw32-make has been broken for quite a while.
When compiling things, always start from the simplest method and increase complexity as the different methods fail. The simplest method is to link sources directly to the program that will use them (as I did above).
If that doesn't work, (e.g. because the sources require configuration done by a configure script, or because the code base is too large or too complex) move on to building a library and linking the program to that library, using the tools the maintainer provided (project files, configure scripts, etc.).
When that doesn't work or it produces undesirable results (e.g. one time, I wanted to link statically to a program as many libraries as possible, but the projects were only building dynamic libraries), move on to creating your own scripts or project files or modifying the existing ones. This is a an arduous process involving a lot of trial and error, playing around with macro definitions, and sometimes even modifying sources, but it gives the absolute best results.
Well, I knew better than to try to compile ConfigFile.cpp itself, but I wasn't linking to it when compiling example.cpp, though Eclipse might have tried that. I think it can only guess what you don't tell it. And I didn't tell it much, mainly because I'm not sure how to, but that's just a matter of reading the manual.
Thank you again for that, I didn't really have a clear understanding of how linking worked.