g++ -Iinc ?

Posted on 2007-11-15
Last Modified: 2011-09-20 xmysql.cpp
CFLAGS=-Iinc -O2 -Wall -W
LIBS=-L/x/mysql/lib -lmysqlclient -lz

.SUFFIXES: .cc .o

$(PROG): $(OBJS)
        $(CC) $(LIBS) $(OBJS) -o $@

        $(CC) $(CFLAGS) -c $<

undefined reference to `mysql_init'
undefined reference to `mysql_real_connect'
undefined reference to `mysql_insert_id'

what does -Iinc do, how can i compile mysql support into this program.

Question by:ed987

Expert Comment

ID: 20295137
"-linc" means link to libinc.a ,it isn't a option of g++ compiler.
Maybe the code should write like this:
CFLAGS=-O2 -Wall -W
LIBS=-L/x/mysql/lib -lmysqlclient -lz -linc
LVL 15

Accepted Solution

efn earned 500 total points
ID: 20295521
-Iinc tells the compiler to look for files referenced in #include directives in a directory named "inc".  This option wouldn't be involved in a linking problem.

I can't tell why you are getting the undefined reference errors.  Are there any other error messages?

Expert Comment

ID: 20295747
sorry ,mis read.
"Iinc" means the path to the including file directory named"inc".
you can create a variable to do this:
INCLUDE_PATH   =   -Iinc

So you can define many other include directories.
>>undefined reference to `mysql_init'
>>undefined reference to `mysql_real_connect'
>>undefined reference to `mysql_insert_id'

Maybe the include path you defined was error, so the complier didn't find the reference to this mysql variables.Make sure your include path is correct to point to the directory of mysql include.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 40

Expert Comment

ID: 20296760
As already stated -Iinc is telling the compiler to include path 'inc' as part of its standard search criteria when including header files that have been included using the #include <> form of the include precompile directive. As it stands the path provided will be relative from where the makefile is run from, so the inc directory must be a sub-directory. If it isn't the compiler will silently ignore this but you'll then get build related errors. That said, the type of build errors I would expect you to see would be telling you it can't find header files so I suspect your include path is probably correct and as such your problem isn't actually related to this.

The error 'undefined reference' is actually a linker error that means it cannot find the definition (body) of a function that has been declared somewhere in your code. This is probably because you either need to link to the object code or library that provides support for that function of your order of inclusion is wrong. With gcc it is necessary to list dependers before dependees, so if A depends on B you must link to A first so the linker knows what to pull out of B. SO, if you are linking to a library that provides mysql support you need to link to that last. This is counter intuitive and catches out a lot of people! Make sure you are linking to the libraries you need in the correct order.

This link should help you resolve this...

Good luck.

LVL 53

Expert Comment

ID: 20296769
Does the /x/mysql/lib directory contain the libmysqlclient.a library ? Usually it's in /usr/lib/mysql
LVL 40

Expert Comment

ID: 20296825
This web page tells you the correct way to link to the mysql libraries -- including discovering where they live...
LVL 40

Expert Comment

ID: 20296829
"When you are linking an application program to use the MySQL client library, you might get undefined reference errors for symbols that start with mysql_, such as those shown here:
/tmp/ccFKsdPa.o: In function `main':
/tmp/ccFKsdPa.o(.text+0xb): undefined reference to `mysql_init'
/tmp/ccFKsdPa.o(.text+0x31): undefined reference to `mysql_real_connect'
/tmp/ccFKsdPa.o(.text+0x57): undefined reference to `mysql_real_connect'
/tmp/ccFKsdPa.o(.text+0x69): undefined reference to `mysql_error'
/tmp/ccFKsdPa.o(.text+0x9a): undefined reference to `mysql_close'

You should be able to solve this problem by adding -Ldir_path -lmysqlclient at the end of your link command, where dir_path represents the pathname of the directory where the client library is located. To determine the correct directory, try this command:

shell> mysql_config --libs

The output from mysql_config might indicate other libraries that should be specified on the link command as well."
LVL 40

Expert Comment

ID: 20338060
Um, the accepted answer didn't fully answer your Q -- other experts assisted in doing that. This should have been closed with a points split!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question