VS 2013在编译C ++程序时出现链接时错误-B. Stroustrup使用C ++的PPP:8-Q1演习? [英] Link-time errors in VS 2013 while compiling the C++ program - B. Stroustrup's PPP using C++: Ch. 8 - Q1 Drill?

查看:72
本文介绍了VS 2013在编译C ++程序时出现链接时错误-B. Stroustrup使用C ++的PPP:8-Q1演习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的副本:
stroustrup ppp第8章钻头


我正在使用B. Stroustrup(第一版)的C ++使用PPP学习C ++.我在解决此问题时遇到了麻烦(错误-LNK2001和LNK1169 ).Q1 8钻-


I'm learning C++ using PPP using C++ by B. Stroustrup, 1st edition. I am having trouble (errors - LNK2001 & LNK1169) solving this question, Ch. 8 Drill, Q1 -

创建三个文件: my.h my.cpp use.cpp .头文件 my.h 包含

extern int foo;
void print_foo();
无效打印(int);

源代码文件 my.cpp#包括my.h std_lib_facilities.h ,定义了 print_foo()以打印以下内容: foo 使用 cout ,并 print(int i)使用 cout < 打印 i 的值/strong>.

The source code file my.cpp #includes my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.

源代码文件 use.cpp #includes my.h ,定义了要设置的 main() foo 的值为 7 并使用 print_foo()打印,并使用 99 进行打印 print().请注意, use.cpp 不会 #include std_lib_facilities.h ,因为它没有直接使用任何这些功能.

The source code file use.cpp #includes my.h, defines main() to set the value of foo to 7 and print it using print_foo(), and to print the value of 99 using print(). Note that use.cpp does not #include std_lib_facilities.h as it doesn't directly use any of those facilities.

编译并运行这些文件.在Windows上,您需要在项目中同时使用 use.cpp my.cpp 并使用 {char cc;cin >> cc;} (位于 use.cpp 中)以查看您的输出.

Get these files compiled and run. On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin>>cc; } in use.cpp to be able to see your output.


我正在使用Visual Studio2013.这是我使用问题创建的文件-


I'm using Visual Studio 2013. Here are the files I created using the question -

my_8drill.h(my.h)

extern int foo;
void print_foo();
void print(int);

my_8drill.cpp(my.cpp)

#include "std_lib_facilities.h"
#include "my_8drill.h"

void print_foo()
{
    cout << foo << endl;
    return;
}

void print(int i)
{
    cout << i << endl;
    return;
}

use_8drill.cpp(use.cpp)

#include <iostream>      // Well, the question never mentioned to add "std_lib_facilities.h", 
                         // so I added this
#include "my_8drill.h"

using std::cin;

int main()
{
    foo = 7;
    print_foo();
    print(99);

    char c;
    while (cin >> c)
        if (c == 'q')
            break;
}

但是这些错误(希望是链接错误)在编译该程序时发生-

But these errors (link errors hopefully) occurred while compiling this program -

1>my_8drill.obj : error LNK2001: unresolved external symbol "int foo" (?foo@@3HA)
1>use_8drill.obj : error LNK2001: unresolved external symbol "int foo" (?foo@@3HA)
1>C:\Users\I$HU\Documents\Visual Studio 2013\Projects\C++ Development\Debug\C++ Development.exe : fatal error LNK1120: 1 unresolved externals

然后我记得 extern int foo; 只是一个声明,而不是定义,因此我需要在某个地方定义它,因此我在头文件 my.h 中将 extern int foo; 替换为 int foo; .

Then I remembered that extern int foo; is only a declaration, not a definition, so I need to define it somewhere, so I replaced extern int foo; with int foo; in the header file my.h.

但是在编译后再次出现这些(链接)错误-

But then again after compiling, these (link) errors occurred -

1>use_8drill.obj : error LNK2005: "int foo" (?foo@@3HA) already defined in my_8drill.obj
1>C:\Users\I$HU\Documents\Visual Studio 2013\Projects\C++ Development\Debug\C++ Development.exe : fatal error LNK1169: one or more multiply defined symbols found

因此,从错误中,我假设由于存在两个"my.h" 标头,一个标头位于 my.cpp 中,另一个标头位于 use.cpp中.,所以我将 my.h 中的 int foo; 改回了 extern int foo; ,并添加了 int foo;改为 use.cpp -

So from the errors, I assumed that since there are two "my.h" headers, one in my.cpp and other in use.cpp, so I changed back int foo; in my.h to extern int foo; and added int foo; to use.cpp like this -

#include <iostream>
int foo;            // this line is new
#include "my_8drill.h"

,因此 foo 不会被定义两次,并且程序现在可以正常运行,

and so foo won't get defined twice, and the program runs fine now,

但是,这些问题再次困扰着我&再次-

But still, these questions are bothering me again & again -

  1. 在解决上述错误的同时我是否纠正了方法?
  2. (扩展为 1 ),关于这些错误(我对此很肯定)是否有更多的信息要知道(缺少详细信息)?
  3. 即使程序最终运行, my.h 中的函数声明如何知道它们的定义(在 my.cpp 中)在哪里?
  4. (扩展为 3 ),当我处于以下状态时,它们如何相互链接( my.h my.cpp )只能 #include d "my.h" use.cpp 吗?
  5. 仅将 int foo; 添加到 my.cpp ,如何让 my.h 知道变量 foo ?
  1. Am I correct in my approach while resolving the above-mentioned errors ?
  2. (Extension to 1) Is there anything more to know (missing details) about these errors (I'm quite positive about this) ?
  3. Even if the program runs in the end, how do function-declarations in my.h know where their definition are (in my.cpp) ?
  4. (Extension to 3) How do they link up (my.h & my.cpp) with each other when I've only #included "my.h" to use.cpp ?
  5. How do just adding int foo; to my.cpp lets my.h know about the variable foo?

请帮助我解决这些问题.

Please help me with these.

这是头文件 std_lib_facilities.h

推荐答案

编译 use.cpp 文件时,编译器仅看到 my.h 头文件而不是包含该文件源代码的 my.cpp 文件.

When you compile the use.cpp file, the compiler only sees the my.h header file and not the my.cpp file which contains the source code of that file.

您还需要在 use.cpp 程序中包含 my.cpp 文件.或者您可以添加

You need to include my.cpp file also in your use.cpp program. Or you can add

#include "my.cpp"

位于 my.h 文件的末尾.

这篇关于VS 2013在编译C ++程序时出现链接时错误-B. Stroustrup使用C ++的PPP:8-Q1演习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆