【问题标题】:How to add gcc include directory to existing Makefile如何将 gcc 包含目录添加到现有的 Makefile
【发布时间】:2015-05-04 11:23:31
【问题描述】:

我有一个 Makefile,我已经复制并在我用 C for 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;
}

【问题讨论】:

  • 那么...有什么问题?请注意,您不应将 -c 添加到 CFLAGS
  • 运行make 的输出是什么?该输出与您的实际意图相比如何?你能不使用make而只在命令行上写命令来编译你的代码吗?
  • 抱歉,用输出和我正在运行的代码更新了问题。还有@MadScientist 为什么我不应该将 -c 包含到 CFLAGS 中?
  • 因为编译的内置规则已经使用-c,所以它是多余的。这也意味着您不能在链接行中使用CFLAGS,这通常是推荐的,因为您希望使用与编译期间使用的大多数相同的调试和优化器标志来调用链接器。一般来说,您不想将选择输出类型的标志放入像CFLAGS 这样的一般变量中;您想将它们直接放入生成该输出的 make 配方中。
  • 请剪切并粘贴 make 调用的编译行,以及准确的错误信息(不要解释)。另外,如果您在运行make 的同一目录中运行ls SQLAPI/include/SQLAPI.h,您会在那里看到文件吗?

标签: c++ linux gcc makefile


【解决方案1】:

好吧,如果你看一下 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)

【讨论】:

    【解决方案2】:

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

    然后使用变量定义CFLAGS

    SQLAPI=/directory/where/sqlapi/is/installed 
    
    CFLAGS =  -I${SQLAPI}/include -Wall -c $(DEBUG)
    

    【讨论】:

    • 我根据您的建议更新了我的问题的 makefile 代码 - 我也尝试了您的建议,但我仍然收到没有此类文件或目录的错误。
    【解决方案3】:

    在您的 MakeFile 中修改 CMAKE_CXX_FLAGS 如下:

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

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多