简体   繁体   English

g++ 中的链接文件

[英]Linking files in g++

Recently I have tried to compile a program in g++ (on Ubuntu).最近我尝试在 g++(在 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.通常我使用 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.带有主 function 的源代码 (.cpp) 文件。

The header file with the function prototypes. header 文件与 function 原型。

The.cpp file with the definitions for each function. .cpp 文件,其中包含每个 function 的定义。

Any help will be appreciated.任何帮助将不胜感激。

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 -o is 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, eg g++ -c *.cpp will 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.总的来说,除非你真的必须在没有任何 IDE 的情况下工作,否则你不应该太担心它。 If you're not that proficient with the command line syntax, stick to IDEs first.如果您不熟悉命令行语法,请先坚持使用 IDE。

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.检查在哪个 cpp 文件中定义了缺少的 class 成员 function。 You may have not given it to gcc.你可能没有把它交给 gcc。

You can also check for correct #include tags within filename.cpp .您还可以在filename.cpp中检查正确的#include标记。 Assume that filename.cpp uses code contained in myclass.h present in the same directory as filename.cpp .假设filename.cpp使用myclass.h中包含的代码,该代码与filename.cpp位于同一目录中。 Assume that the class that g++ says is undefined is contained in myclass.h and defined in myclass.cpp .假设 g++ 所说的未定义的 class 包含在myclass.h中并在myclass.cpp中定义。 So, to correctly include myclass.h within filename.cpp , do the following:因此,要在filename.cpp中正确包含myclass.h ,请执行以下操作:

  1. In filename.cpp :filename.cpp中:
#include <iostream>  
#include <myclass.h>  
//..source code.  
  1. In the makefile:在 makefile 中:
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.在上面,请注意-I. option when compiling filename.cpp .编译filename.cpp时的选项。 The -I<directory> asks g++ to include the path following the -I part into the search path. -I<directory>要求g++-I部分之后的路径包含到搜索路径中。 That way myclass.h is correctly included.这样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.所有的尝试都将只是在黑暗中刺伤。

I assume that you have declared a member function (usually in a .h or .hpp file) but have ommited the respective definition of the member function (usually in a .cpp file).我假设您已经声明了成员 function(通常在.h.hpp文件中),但已经省略了成员 function 的相应定义(通常在.cpp文件中)。

In c++, it is possible to declare a class like so:在 c++ 中,可以像这样声明 class:

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.只要在整个编译代码中没有引用成员 function foo::y() ,这对编译/链接过程就没有问题。

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

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