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

标签 c++ linux gcc 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;
}

最佳答案

好吧,如果您查看 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)

关于c++ - 如何将 gcc include 目录添加到现有的 Makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838079/

相关文章:

c++ - 高效/可移植的有符号整数模正整数返回非负数?

c++ - 无法实现光子映射

linux - 用于查找路由器是否繁忙的 SNMP MIB

gcc - GCC何时插入异或eax,eax?

macos - 在 mac os x 中使用 qtcreator 和 gnu g++4.8

c++ - boost 不可复制的怪异性

c++ - 人们是否能够使用自己的实现来劫持已编译的库?

python - iwlist 扫描输出格式

linux - 我可以在 linux 平台上构建我的 .net-core 应用程序,然后部署到 azure web 应用程序吗

c - 浮点乘法与重复加法