简体   繁体   English

高级C和C++编译:无法编译书中提到的示例程序

[英]Advanced C and C++ compiling: unable to compile the example program mentioned in the book

I'm reading the book named "Advanced C and C++ compiling", by Milan Stevanovic我正在阅读 Milan Stevanovic 写的名为“高级 C 和 C++ 编译”的书

The following is the snapshot from the book, followed by the problem I'm facing.以下是书中的快照,然后是我面临的问题。


Concept illustration: Demo Project概念图:演示项目

The development environment used to build this simple project will be based on the gcc compiler running on Linux.用于构建这个简单项目的开发环境将基于在 Linux 上运行的 gcc 编译器。 Listings 2-1 through 2-3 contain the code used in the demo project.清单 2-1 到 2-3 包含演示项目中使用的代码。

Listing 2-1.清单 2-1。 function.h

#pragma once
#define FIRST_OPTION
#ifdef FIRST_OPTION
#define MULTIPLIER (3.0)
#else
#define MULTIPLIER (2.0)
#endif

float add_and_multiply(float x, float y);

Listing 2-2.清单 2-2。 function.c

int nCompletionStatus = 0;
float add(float x, float y)
{
    float z = x + y;
    return z;
}
float add_and_multiply(float x, float y)
{
    float z = add(x,y);
    z *= MULTIPLIER;
    return z;
}

Listing 2-3.清单 2-3。 main.c

#include "function.h"
extern int nCompletionStatus = 0;
int main(int argc, char* argv[])
{
    float x = 1.0;
    float y = 5.0;
    float z;
    z = add_and_multiply(x,y);
    nCompletionStatus = 1;
    return 0;
}

Demo Project Preprocessing Example:演示项目预处理示例:

The gcc compiler provides the mode in which only the preprocessing stage is performed on the input source files: gcc 编译器提供了只对输入源文件执行预处理阶段的模式:

gcc -i <input file> -o <output preprocessed file>.i

Unless specified otherwise, the output of the preprocessor is the file that has the same name as the input file and whose file extension is .i .除非另有说明,预处理器的输出是与输入文件同名且文件扩展名为.i的文件。 The result of running the preprocessor on the file function.c looks like that in Listing 2-4.对文件function.c运行预处理器的结果如清单 2-4 所示。

Listing 2-4.清单 2-4。 function.i

# 1 "function.c"
# 1 "
# 1 "
# 1 "function.h" 1
# 11 "function.h"
float add_and_multiply(float x, float y);
# 2 "function.c" 2
int nCompletionStatus = 0;
float add(float x, float y)
{
    float z = x + y;
    return z;
}
float add_and_multiply(float x, float y)
{
    float z = add(x,y);
    z *= MULTIPLIER;
    return z;
}

More compact and more meaningful preprocessor output may be obtained if few extra flags are passed to the gcc, like如果向 gcc 传递很少的额外标志,则可以获得更紧凑和更有意义的预处理器输出,例如

gcc -E -P -i <input file> -o <output preprocessed file>.i

which results in the preprocessed file seen in Listing 2-5.这导致了清单 2-5 中看到的预处理文件。

Listing 2-5.清单 2-5。 function.i (Trimmed Down Version) function.i (精简版)

float add_and_multiply(float x, float y);
int nCompletionStatus = 0;
float add(float x, float y)
{
    float z = x + y;
    return z;
}
float add_and_multiply(float x, float y)
{
    float z = add(x,y);
    z *= 3.0;
    return z;
}

Obviously, the preprocessor replaced the symbol MULTIPLIER , whose actual value, based on the fact that the USE_FIRST_OPTION variable was defined, ended up being 3.0 .显然,预处理器替换了符号MULTIPLIER ,基于USE_FIRST_OPTION变量被定义的事实,其实际值最终为3.0


Problem:问题:

When I compile the program as is using gcc, following is the error I am facing Snapshot from my terminal.当我像使用 gcc 一样编译程序时,以下是我在终端中遇到的 Snapshot 错误。

gcc -i function.c -o function.i
cc1: error: unrecognized command line option '-i'

gcc function.c -o function.i
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:
In function '_start':
(.text+0x18): undefined reference to 'main'
collect2: ld returned 1 exit status

$pwd
/home/adminuser/advance_compiling
$ll
total 20
drwxrwxr-x  2 adminuser adminuser 4096 Jan 10 23:51 ./
drwxr-xr-x 26 adminuser adminuser 4096 Jan 10 23:57 ../
-rw-rw-r--  1 adminuser adminuser  216 Nov 15 08:58 function.c
-rw-rw-r--  1 adminuser adminuser  163 Jan 10 23:33 function.h
-rw-rw-r--  1 adminuser adminuser  257 Dec 28 06:46 main.c

How do I get rid of this and proceed in learning the course?我如何摆脱这种情况并继续学习课程? Please suggest.请建议。

-i is not a valid command line option - I'm not sure where the book got it from. -i不是有效的命令行选项 - 我不确定这本书是从哪里得到的。 In order to just run the preprocessor, you should use the option -E instead.为了只运行预处理器,您应该改用选项-E

I was a technical reviewer of the book and the author asked me to post this explanation on his behalf:我是这本书的技术审阅者,作者让我代表他发表这个解释:

The error you found is indeed an error, introduced in the very late stage of book proofreading/editing.你发现的错误确实是一个错误,在书籍校对/编辑的最后阶段引入的。 Sorry for stepping on it + for book errata still not being printed out (will happen some time soon).很抱歉踩到了它+书勘误仍未打印出来(很快就会发生)。

Try this尝试这个

gcc -E -P function.c -o function.i

without the -i option, it's not a gcc option.没有-i选项,它不是gcc选项。

note : this answer was fixed after I realized that have misunderstood the question, so i fixed to keep it and not delete it, but Katie did answer it before I did.注意:这个答案是在我意识到误解了问题后修复的,所以我固定保留它而不是删除它,但凯蒂确实在我之前回答了它。

Judging by the .i output file in the book's example, it seems the error is in the utility itself.从书中示例中的.i输出文件来看,错误似乎出在实用程序本身中。

Instead of using gcc , use cpp .不要使用gcc ,而是使用cpp gcc uses cpp (The C Preprocessor) under the hood to handle macro expansion and header inclusion. gcc 在底层使用 cpp(C 预处理器)来处理宏扩展和头文件包含。 The output of cpp is a .i file for c source code and .ii for c++. cpp 的输出是 c 源代码的.i文件和 c++ 的.ii文件。

cpp function.c > function.i

Try the following code:试试下面的代码:

function.c

#include "function.h"
int nCompletionStatus = 0;
float add(float x, float y)
{
    float z = x + y;
    return z;
}
float add_and_multiply(float x, float y)
{
    float z = add(x,y);
    z *= MULTIPLIER;
    return z;
}

main.c

    //#include "function.h"
    extern int nCompletionStatus;
    int main(int argc, char* argv[])
    {
        float x = 1.0;
        float y = 5.0;
        float z;
        z = add_and_multiply(x,y);
        nCompletionStatus = 1;
        return 0;
    }

Now execute:
gcc function.c main.c

otherwise 
gcc -c function.c
gcc function.o main.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM