1

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;
}
Community
  • 1
  • 1
dimlee
  • 452
  • 1
  • 10
  • 22
  • 1
    So... what's the problem? Note you should not add `-c` to `CFLAGS`. – MadScientist Mar 03 '15 at 17:28
  • What is the output from running `make`? How does that output compare to what you actually intended? Can you compile your code without using `make` and instead just by writing command on the command line? – Bill Lynch Mar 03 '15 at 17:30
  • Sorry about that, updated the question with output and the code I"m running. Also @MadScientist why should I not include -c to CFLAGS? – dimlee Mar 03 '15 at 17:34
  • Because the built-in rule for compiling uses `-c` already so it's redundant. It also means you can't use `CFLAGS` in the link line, which is usually recommended because you want to call the linker with most of the same debug and optimizer flags you use during compilation. In general you don't want to put flags that select the type of output into a general variable like `CFLAGS`; you want to put them directly into the make recipe that generates that output. – MadScientist Mar 03 '15 at 17:42
  • Please cut and paste the compile line that make invokes, and the exact error message (don't paraphrase). Also, if you run `ls SQLAPI/include/SQLAPI.h` in the same directory where you run `make` do you see the file there, or not? – MadScientist Mar 03 '15 at 17:44
  • @MadScientist I copied and pasted everything that I can see. – dimlee Mar 03 '15 at 17:56

3 Answers3

5

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)
MadScientist
  • 92,819
  • 9
  • 109
  • 136
0

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)
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I updated my question's makefile code with your suggestion - I also tried your suggestion but im still getting the no such file or directory error. – dimlee Mar 03 '15 at 17:47
0

In your MakeFile modify CMAKE_CXX_FLAGS as below:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/path/to/your/folder")
Kartik Javali
  • 327
  • 1
  • 4
  • 11