c++ - 编译 C++ 程序时 VS 2013 中的链接时错误 - B. Stroustrup's PPP using C++ : Ch. 8 - Q1 Drill?

标签 c++ visual-studio visual-c++ compiler-errors linker

Possible duplicate:
stroustrup ppp chapter 8 drill headers



我正在使用 B. Stroustrup 的 C++ 使用 PPP 学习 C++,第 1 版。我在解决这个问题时遇到了麻烦( 错误 - LNK2001 & LNK1169 ), channel 8 钻,第一季度 ——

Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains

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



源代码文件 my.cpp #includes my.h std_lib_facilities.h , 定义 print_foo() 打印 的值foo 使用 cout , 和 打印(int i)打印 的值我 使用 cout .

源代码文件 use.cpp #includes my.h , 定义 main() 设置
的值foo 7 并使用 打印它print_foo() , 并打印 的值99 使用 打印() .请注意 使用.cpp #include std_lib_facilities.h 因为它不直接使用任何这些设施。

编译并运行这些文件。在 Windows 上,你需要有
两者 使用.cpp my.cpp 在项目中使用 { 字符 cc; cin>>cc; } 使用.cpp 能够看到你的输出。


我正在使用 Visual Studio 2013。这是我使用问题创建的文件 -

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;
}

但是这些错误 (希望链接错误)在编译这个程序时发生 -
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;只是声明,不是定义,所以我需要在某处定义它,所以我替换了extern int foo;int foo;在头文件中 my.h .

但是在编译之后,这些(链接)错误 发生了 -
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 和其他在 使用.cpp ,所以我改回来 int foo; my.h extern int foo;并添加 int foo;使用.cpp 像这样 -
#include <iostream>
int foo;            // this line is new
#include "my_8drill.h"

等等 foo不会被定义两次,程序现在运行良好,

但是,这些 问题 一次又一次地困扰着我——
  • 在解决上述错误时,我的方法是否正确?
  • (扩展到 1 )关于这些错误还有什么需要了解的(缺少细节)(我对此非常肯定)?
  • 即使程序运行到最后,中的function-declarations怎么办? my.h 知道它们的定义在哪里(在 my.cpp 中)?
  • (扩展到 3 )它们如何链接( my.h & my.cpp )仅当 I've4019|71119 #include d "my.h"使用.cpp ?
  • 怎么加才int foo; my.cpp my.h 了解变量 foo ?

  • 请帮我解决这些问题。

    这是头文件 std_lib_facilities.h .

    最佳答案

    当你编译 use.cpp文件,编译器只能看到 my.h头文件而不是 my.cpp包含该文件源代码的文件。

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

    #include "my.cpp"
    

    my.h文件。

    关于c++ - 编译 C++ 程序时 VS 2013 中的链接时错误 - B. Stroustrup's PPP using C++ : Ch. 8 - Q1 Drill?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191942/

    相关文章:

    c++ - 读写二进制文件

    c++ - 关于 C++ 中内存释放的困惑

    c++ - Windows __stdcall DLL 的 DEF 文件格式? (*.def -> lib.exe -> *.lib)

    winapi - 更改使用 Createwindow 命令创建的按钮的背景颜色

    c++ - 我们可以将对象存储在文件中以供以后检索吗?

    c++ - 如何将 "normal"C++ 类转换为模板?

    git - 在Visual Studio Professional 2017中提交一个文件

    c++ - open62541:引用函数的链接器工具错误

    asp.net - 元素 'RadGrid' 不是已知元素

    c++ - VC++2010 中的编译器错误(2008 年干净!), "ambiguous call to overloaded function"