如何将 gcc include 目录添加到现有 Makefile

dim*_*lee 1 c++ linux gcc makefile

我有一个 Makefile,我已将其复制并在我用 C for Linux 编写的许多小程序中使用。遗憾的是,我不了解它如何工作的每个细节,我通常只是注释掉输出文件的名称并插入我想要的名称,它就可以成功编译我的程序。我想使用这些说明:

使用命令行编译器的用户通常会使用“/I%SQLAPIDIR%\include”或“-I${SQLAPIDIR}/include”等选项。头文件位于 SQLAPI++ 发行版的 include 子目录中

这样我的 Makefile 就会在编译时添加库。我检查了这个网站并找到了以下链接,但它们没有帮助:

GCC 默认包含目录是什么?

如何向现有 makefile 项目添加新文件

将包含目录添加到 gcc *before* -I

我尝试包含该目录......

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
Run Code Online (Sandbox Code Playgroud)

当我运行下面的代码时,出现错误:

[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
Run Code Online (Sandbox Code Playgroud)

目录是这样的:

[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
Run Code Online (Sandbox Code Playgroud)

SQLAPI目录如下:

[developer@localhost testql]$ ls SQLAPI/include/SQLAPI.h
SQLAPI/include/SQLAPI.h
Run Code Online (Sandbox Code Playgroud)

代码...

 #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;
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*ist 5

好吧,如果你看看 make 调用的编译行,问题就很清楚了:

g++    -c -o testql.o testql.cpp
Run Code Online (Sandbox Code Playgroud)

这里没有-I旗帜。问题在于该CFLAGS变量是用于编译C代码的,但你正在编译C++代码。如果要设置特定于 C++ 编译器的标志,则需要设置CXXFLAGS.

但是,对于所有预处理器标志,您应该使用CPPFLAGS; C 和 C++ 编译器(以及调用预处理器的其他工具)都使用它。因此请使用:

CPPFLAGS = -I${SQLAPI}/include
CFLAGS = -Wall $(DEBUG)
CXXFLAGS = $(CFLAGS)
Run Code Online (Sandbox Code Playgroud)