C++ 在 g++ 中链接文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6532400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:19:31  来源:igfitidea点击:

Linking files in g++

c++g++compiler-errorslinker-errors

提问by E.O.

Recently I have tried to compile a program in g++ (on Ubuntu). Usually i use Dev-C++ (on Windows) and it works fine there as long as I make a project and put all the necessary files in there.

最近我尝试用 g++(在 Ubuntu 上)编译一个程序。通常我使用 Dev-C++(在 Windows 上),只要我制作一个项目并将所有必要的文件放在那里,它就可以正常工作。

The error that occurs when compiling the program is:

编译程序时出现的错误是:

$filename.cpp: undefined reference to '[Class]::[Class Member Function]'

The files used are as following:

使用的文件如下:

The source code (.cpp) file with the main function.

带有 main 函数的源代码 (.cpp) 文件。

The header file with the function prototypes.

带有函数原型的头文件。

The .cpp file with the definitions for each function.

包含每个函数定义的 .cpp 文件。

Any help will be appreciated.

任何帮助将不胜感激。

回答by Mario

You probably tried to either compile and link instead of just compiling source files or somehow forgot something.

您可能试图编译和链接而不是仅仅编译源文件,或者以某种方式忘记了某些东西。

Variation one (everything in one line; recompiles everything all the time):

变体一(所有内容都在一行中;一直重新编译所有内容):

g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]

Variation two (step by step; if no -ois provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):

变体二(一步一步;如果没有-o提供,gcc 将重用输入文件名,并在不链接时只更改扩展名;这种变体最适合用于 makefile;允许您跳过未更改的部分):

g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]

Variation three (some placeholders):

变体三(一些占位符):

Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cppwill compile all cpp files in current directory to o(bject) files of the same name.

就不一一列举了,但上面提到的参数也可以占位符,例如g++ -c *.cpp将当前目录中的所有cpp文件编译为同名的o(bject)文件。

Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.

总的来说,除非您真的必须在没有任何 IDE 的情况下工作,否则您不必担心太多。如果您不精通命令行语法,请先使用 IDE。

回答by Rémi

The command line of gcc should look like:

gcc 的命令行应如下所示:

g++ -o myprogram class1.cpp class2.cpp class3.cpp main.cpp

Check in which cpp file the missing class member function is defined. You may have not given it to gcc.

检查在哪个 cpp 文件中定义了缺少的类成员函数。你可能没有把它交给 gcc。

回答by Sriram

You can also check for correct #includetags within filename.cpp. Assume that filename.cppuses code contained in myclass.hpresent in the same directory as filename.cpp. Assume that the class that g++ says is undefined is contained in myclass.hand defined in myclass.cpp. So, to correctly include myclass.hwithin filename.cpp, do the following:

您也可以检查其是否#include内标签filename.cpp。假设filename.cpp使用的代码myclass.h存在于与filename.cpp. 假设 g++ 所说的未定义类包含myclass.hmyclass.cpp. 因此,要正确包含myclass.h在 中filename.cpp,请执行以下操作:

  1. In filename.cpp:
  1. filename.cpp
#include <iostream>  
#include <myclass.h>  
//..source code.  
  1. In the makefile:
  1. 在生成文件中:
filename.o: myclass.C myclass.h filename.cpp
g++ -I./ -c filename.cpp -o filename.o

myclass.o: myclass.C myclass.h  
g++ -c myclass.C -o myclass.o

In the above, note the use of -I.option when compiling filename.cpp. The -I<directory>asks g++to include the path following the -Ipart into the search path. That way myclass.his correctly included.

以上,-I.编译时注意option的使用filename.cpp。该-I<directory>要求g++包括以下的路径-I部分入搜索路径。这种方式myclass.h被正确地包括在内。

In the absence of more information (the source maybe), it is difficult to say with any accuracy where the problem lies. All attempts will be but stabs in the dark.

在没有更多信息(可能是来源)的情况下,很难准确地说出问题出在哪里。所有的尝试都只是在黑暗中刺伤。

回答by René Nyffenegger

I assume that you have declared a member function (usually in a .hor .hppfile) but have ommited the respective definition of the member function (usually in a .cppfile).

我假设您已经声明了一个成员函数(通常在 a.h.hpp文件中),但省略了成员函数的相应定义(通常在.cpp文件中)。

In c++, it is possible to declare a class like so:

在 C++ 中,可以像这样声明一个类:

class foo {
  void x();
  void y();
}

with a cpp file that goes like so

带有一个像这样的 cpp 文件

void foo::x() {
   do_something()
}

Note, there is no foo::y().

请注意,没有foo::y().

This poses no problem to the compiling/linking process as long as the member function foo::y()is referenced nowhere throughout the compiled code.

只要在foo::y()整个编译代码中没有引用成员函数,这对编译/链接过程就没有问题。