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

How to add gcc include directory to existing Makefile

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

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

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

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

我尝试包含目录....

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

当我 运行 下面的代码时,我得到了错误:

[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

目录如下:

[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

SQLAPI 目录如下:

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

代码...

 #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;
}

定义变量 SQLAPI,使其值为安装 SQLAPI 的目录。

然后用变量定义CFLAGS.

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

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

嗯,如果您查看 make 正在调用的编译行,就会很清楚问题出在哪里:

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

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

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

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

在您的 MakeFile 中修改 CMAKE_CXX_FLAGS 如下:

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