C++ – How to tell C++ library path in Cygwin and MinGW

ccompiler-constructioncygwinmingw

I develop C++ programs using a Cygwin installation on top of Windows XP.

I also have MinGW installed, because I want to use it's version of g++, not the one that comes with Cygwin.

That part seems to be set up correctly. When I start a Cygwin session I see this:

$ which g++
/cygdrive/c/MinGW/bin/g++

This is correct, g++ is pointing to my MinGW install.

What I don't understand is when I write code that includes library code (for example, header files from the `Winsock/BerkleySockets API), how can I tell where the compiler is finding that header file?

For example, if I have #include "winsock.h" in my code, where does the compiler find that header file?

If I do a general search for winsock.h on my computer, I get this:

C:\MinGW\include
C:\cygwin\usr\include\w32api

Both have a copy of winsock.h (though the file sizes of these aren't exactly the same, so they can't be identical).

Thanks for the help.

I should also point out, I have the C:\MinGW\bin in my Windows PATH Environment Variable, as well as that same path configured in my/etc/profile file within Cygwin.

Best Answer

I'm guessing the g++ compiled for MingW has the same command line arguments as the standard g++. Check out the g++ manual page.

To add include paths to your compilation, use the -I flag.

g++ -I/include/path/here -I/another/include/path -o prog src.cpp

To add library paths to your linking, use the -L flag.

g++ -L/lib/path/here -L/another/lib/path -o prog src.cpp

The MingW site explains how the include file search works on MingW, and how to modify it.

The site also says that if you want to view the include file search while it happens during the compilation, pass the verbose flag (-v) to the compiler.

g++ -v -o prog src.cpp