【发布时间】: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,您会在那里看到文件吗?