使用MinGW单独编译

发布于 2024-12-29 19:18:55 字数 1942 浏览 4 评论 0原文

使用本教程 Makefile 我想构建一个带有单独编译的简单程序,主要问题是IDE Eclpse Indigo C\C++(prespective)或MinGW无法编译文件。我得到的错误是:

undefined reference to double getAverage<int, 85u>(int (&) [85u])'
undefined reference to int getMax<int, 85u>(int (&) [85u])'
undefined reference to int getMin<int, 85u>(int (&) [85u])'
undefined reference to void print<int, 85u>(int (&) [85u])'
undefined reference to void sort<int, 85u>(int (&) [85u])'
undefined reference to void print<int, 85u>(int (&) [85u])'

main.cpp 文件是这样的:

#include "Tools.h"
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
int numbers[] = {1,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8};
cout <<"Average = "<< getAverage(numbers) << endl;
cout <<"Max element = "<< getMax(numbers) << endl;
cout <<"Minimal element = "<< getMin(numbers) << endl;
print(numbers);
sort(numbers);
print(numbers);
return 0;
}

并且我有一个 Tools.h 文件:

#ifndef TOOLS_H_
#define TOOLS_H_
#include <iostream>
int getBigger(int numberOne,int numberTwo);
template <typename T,size_t N> double getAverage(T (&numbers)[N]);
template <typename T,size_t N> T getMax(T (&numbers)[N]);
template <typename T,size_t N> T getMin(T (&numbers)[N]);
/**
 * Implementing a simple sort method of big arrays
 */
template <typename T,size_t N> void sort(T (&numbers)[N]);
/**
 * Implementing a method for printing arrays
 */
template <typename T,size_t N> void print(T (&numbers)[N]);
#endif

Using this tutorial Makefile I want to build a simple program with a separate compiling, The main problem is that the IDE Eclpse Indigo C\C++ (prespective) or MinGW I cannot compile the files. The error which I get is :

undefined reference to double getAverage<int, 85u>(int (&) [85u])'
undefined reference to int getMax<int, 85u>(int (&) [85u])'
undefined reference to int getMin<int, 85u>(int (&) [85u])'
undefined reference to void print<int, 85u>(int (&) [85u])'
undefined reference to void sort<int, 85u>(int (&) [85u])'
undefined reference to void print<int, 85u>(int (&) [85u])'

The main.cpp file is this :

#include "Tools.h"
#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
int numbers[] = {1,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8,-2,7,14,5,6,16,8};
cout <<"Average = "<< getAverage(numbers) << endl;
cout <<"Max element = "<< getMax(numbers) << endl;
cout <<"Minimal element = "<< getMin(numbers) << endl;
print(numbers);
sort(numbers);
print(numbers);
return 0;
}

and I have a Tools.h file :

#ifndef TOOLS_H_
#define TOOLS_H_
#include <iostream>
int getBigger(int numberOne,int numberTwo);
template <typename T,size_t N> double getAverage(T (&numbers)[N]);
template <typename T,size_t N> T getMax(T (&numbers)[N]);
template <typename T,size_t N> T getMin(T (&numbers)[N]);
/**
 * Implementing a simple sort method of big arrays
 */
template <typename T,size_t N> void sort(T (&numbers)[N]);
/**
 * Implementing a method for printing arrays
 */
template <typename T,size_t N> void print(T (&numbers)[N]);
#endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌生 2025-01-05 19:18:55

当您编译 Tools.cpp 时,编译器不知道您在 main.cpp 中使用的模板参数。因此它不会编译与该模板相关的任何内容。

您需要包含来自使用它们的编译单元的这些模板定义。文件 Tools.cpp 通常被重命名为 Tools.inl 之类的名称,以表明它既不是头文件也不是单独的编译单元。

编译单元“main.cpp”可能如下所示:

#include "tools.h"
#include "tools.inl"

main()
{
    int number[] = {1,2,3};
    getaverage(numbers);
}

由于编译器识别所需的专业化,因此它可以从实现文件生成代码。

When you compile Tools.cpp your compiler has no idea about the template parameters that you have used in main.cpp. Therefore it compiles nothing related to this templates.

You need to include theses template definitions from the compilation unit that uses them. The file Tools.cpp is often renamed to something like Tools.inl to indicate that it's neither a header file nor a separate compilation unit.

The compilation unit "main.cpp" could look like this:

#include "tools.h"
#include "tools.inl"

main()
{
    int number[] = {1,2,3};
    getaverage(numbers);
}

Since the compiler identifies the required specialization it can generate the code from the implementation file.

对不⑦ 2025-01-05 19:18:55

对于大多数情况,哈珀的回答是合适的。但为了完整起见,还应该提及显式模板实例化。

当您在每个编译单元中包含实现时,您的模板类和函数将在所有编译单元中实例化和编译。有时,这是不可取的。如果您的模板类和函数非常复杂,这主要是由于编译时内存限制和编译时间造成的。当您或您使用的库严重依赖模板元编程时,这将成为一个非常现实的问题。另一种情况可能是您的模板函数实现可能在许多编译单元中使用,当您更改实现时,您将被迫重新编译所有这些编译单元。

因此,这些情况下的解决方案是包含一个像 tools.h 这样的头文件,并有一个 tools.cpp 来实现模板。问题是,您应该为将在整个程序中使用的所有模板参数显式实例化模板。这是通过将以下内容添加到 tools.cpp 中来完成的:

template double getAverage<int,85>(int (&numbers)[85]);

注意:显然您必须对“85”执行一些操作,例如在头文件中定义它并在 tools 中使用它。 cpp 和 main.cpp

For most cases, harper's answer is appropriate. But for completeness' sake, explicit template instantiation should also be mentioned.

When you include the implementation in every compilation unit, your template classes and functions will be instantiated and compiled in all of them. Sometimes, this is not desirable. It is mostly due to compile-time memory restrictions and compilation time, if your template classes and functions are very complicated. This becomes a very real issue when you, or the libraries you use rely heavily on template metaprogramming. Another situation could be that your template function implementations might be used in many compilation units, and when you change the implementation, you will be forced to re-compile all those compilation units.

So, the solution in these situations is to include a header file like your tools.h, and have a tools.cpp, implementing the templates. The catch is that, you should explicitly instantiate your templates for all the template arguments that will be used throughout your program. This is accomplished via adding the following to tools.cpp:

template double getAverage<int,85>(int (&numbers)[85]);

Note: You obviously have to do something about that "85", such as defining it in a header file and using it across tools.cpp and main.cpp

好菇凉咱不稀罕他 2025-01-05 19:18:55

我发现这篇文章很有用: 模板和头文件

我声明了该函数在 Tools.h 文件中并包含 Tool.hpp 文件,之后我在 Tools.hpp 文件中定义它们。

I've found this article which is useful : templates and header files

I declared the function in the Tools.h file and include there the file Tool.hpp and after this I defined them in the Tools.hpp file.

最美不过初阳 2025-01-05 19:18:55

我还没有尝试将 .cpp 和 .c 文件一起编译,但也许我的示例会有所帮助。

我在使用标准 gcc 的 mingw 上编译两个单独的程序集文件 .s 时遇到类似的问题
编译器,我实现如下:

gcc -m32 -o test test.s hello.s

-m32 意味着我正在编译 32 位代码

-o 是输出文件(在我的示例中是“测试”文件)

test.s 和 hello.s 是我的源文件。 test.s 是主文件,hello.s 具有辅助函数。 (哦,值得一提的是,这两个文件位于同一目录中)

I haven't tried to compile .cpp and .c files together but maybe my example will help.

I had similar problem compiling two separate assembly files .s on mingw with standard gcc
compiler and i achieved it as follows:

gcc -m32 -o test test.s hello.s

-m32 means i'm compiling 32bit code

-o is the output file ( which in my example is the "test" file )

test.s and hello.s are my source files. test.s is the main file and hello.s has the helper function. (Oh, to mention is the fact that both files are in the same directory)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文