4

I'm new to gcc, and trying to compile a c++ program which includes mysql.h using the command:

g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql

It works without issue, but I was wondering if someone could explain the arguments to me. I don't like using commands I don't understand.

Thanks

wyatt
  • 3,188
  • 10
  • 36
  • 48

3 Answers3

4

-o test means the output file is to be named "test".

test.cpp is your source file, of course.

-L/usr/include/mysql means to look for libraries in /usr/include/mysql, as well as in the usual link path. (It probably isn't finding any libraries here; my libmysqlclient.a is in the standard library directory /usr/lib. So I don't think you need this option.)

-lmysqlclient means to link with the mysqlclient library (actually named libmysqlclient.a)

-I/usr/include/mysql means to look for #include files in /usr/include/mysql, as well as in the usual include path.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • If I'm not wrong some of them are only required when the environment macros such as LD_LIBRARY_PATH , PATH are either not set or set to different path than you are expecting. – dicaprio May 06 '10 at 05:20
  • So is his use of `-lmysqlclient` _wrong_ and should he write `-llibmysqlclient.a` instead? – bobobobo Apr 11 '13 at 20:07
  • 1
    @bobobobo: No. The linker adds the `lib` to the beginning and the `.a` at the end. – Fred Larson Apr 11 '13 at 20:14
  • [Just noting I had a hell of a nightmare earlier](http://stackoverflow.com/questions/15958612/libmysqlclient-a-is-nowhere-to-be-found/15958889#15958889) with not having the `.a` file on my system – bobobobo Apr 11 '13 at 22:37
1

try "man g++" for a full description of what the various options mean.

hookenz
  • 36,432
  • 45
  • 177
  • 286
0

man gcc will give you the details of all these options.

g++ -o test test.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql

g++ : the compiler
-o test : name the resulting binary "test" 
test.cpp : your source file
-L : the directory to look in for libraries (that are specified by -l)
-l : named library to link against (looks for it in -L)
-I : the directory to look in for #included header files
Stephen
  • 47,994
  • 7
  • 61
  • 70