dimlee
dimlee

Reputation: 462

How to add gcc include directory to existing Makefile

I have a Makefile that I have copied and use in many small programs that I write in C for Linux. Sadly, I don't understand every detail of how it works and I typically just comment out the name of the output files and insert the name I want and it compiles my programs successfully. I would like to use these instructions:

Those with a command-line compiler will typically use options such as '/I%SQLAPIDIR%\include' or '-I${SQLAPIDIR}/include'. The header files are in the include subdirectory of SQLAPI++ distributions

so that my Makefile will add the libraries when it compiles. I checked this site and found the following links but they didn't help:

What are the GCC default include directories?

how to add a new files to existing makefile project

Adding an include directory to gcc *before* -I

My attempt at including the directory....

OBJS =  testql.o
CC = g++
DEBUG = -g
SQLAPI=/home/developer/Desktop/ARC_DEVELOPER/user123/testsql/SQLAPI
CFLAGS = -I${SQLAPI}/include -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)

testql: $(OBJS)
    $(CC) $(LFLAGS) $(OBJS) -o testql

clean:
    rm -f testql *.o *~ core

When I run the code below I get the error:

[developer@localhost testql]$ make
g++    -c -o testql.o testql.cpp
testql.cpp:2:44: fatal error: SQLAPI.h: No such file or directory
#include <SQLAPI.h> // main SQLAPI++ header

The directory is as such:

[developer@localhost testql]$ ls -l
total 12
-rw-rw-r--. 1 developer developer  286 Mar  3 12:47 Makefile
drwxr-xr-x. 7 developer developer 4096 Oct 16 02:08 SQLAPI
-rw-rw-r--. 1 developer developer 1169 Mar  3 11:43 testql.cpp

And the SQLAPI directory is as such:

[developer@localhost testql]$ ls SQLAPI/include/SQLAPI.h
SQLAPI/include/SQLAPI.h

code...

 #include <stdio.h>  // for printf
  #include <SQLAPI.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
SAConnection con; // create connection object

try
{
    // connect to database
    // in this example it is Oracle,
    // but can also be Sybase, Informix, DB2
    // SQLServer, InterBase, SQLBase and ODBC
    con.Connect(
        "test",     // database name
        "tester",   // user name
        "tester",   // password
        SA_Oracle_Client);

    printf("We are connected!\n");

    // Disconnect is optional
    // autodisconnect will ocur in destructor if needed
    con.Disconnect();

    printf("We are disconnected!\n");
}
catch(SAException &x)
{
    // SAConnection::Rollback()
    // can also throw an exception
    // (if a network error for example),
    // we will be ready
    try
    {
        // on error rollback changes
        con.Rollback();
    }
    catch(SAException &)
    {
    }
    // print error message
    printf("%s\n", (const char*)x.ErrText());
}

return 0;
}

Upvotes: 1

Views: 6092

Answers (3)

Kartik Javali
Kartik Javali

Reputation: 327

In your MakeFile modify CMAKE_CXX_FLAGS as below:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/path/to/your/folder")

Upvotes: 0

MadScientist
MadScientist

Reputation: 100781

Well, it's quite clear what the problem is if you look at the compile line that make is invoking:

g++    -c -o testql.o testql.cpp

There is no -I flag here. The problem is that the CFLAGS variable is for compiling C code, but you are compiling C++ code. If you want to set flags specific to the C++ compiler you need to set CXXFLAGS.

However, for all preprocessor flags you should use CPPFLAGS; that's used by both the C and C++ compilers (and by other tools that invoke the preprocessor as well. So use:

CPPFLAGS = -I${SQLAPI}/include
CFLAGS = -Wall $(DEBUG)
CXXFLAGS = $(CFLAGS)

Upvotes: 5

R Sahu
R Sahu

Reputation: 206557

Define the variable SQLAPI such that its value is the directory where SQLAPI is installed.

Then use the variable to define CFLAGS.

SQLAPI=/directory/where/sqlapi/is/installed 

CFLAGS =  -I${SQLAPI}/include -Wall -c $(DEBUG)

Upvotes: 0

Related Questions