简体   繁体   English

C++ 无法编译简单的类头(链接器命令失败,退出代码为 1)

[英]C++ Can't compile simple class header (linker command failed with exit code 1)

the problem seems to be with the compiler I'm using though I'm fairly new to programming so I'm not sure how to mess with that(I'm using VSCode on Mac OSX)问题似乎出在我使用的编译器上,尽管我对编程还很陌生,所以我不确定如何解决这个问题(我在 Mac OSX 上使用 VSCode)

This is my header:这是我的标题:

#ifndef STICKMAN_H
#define STICKMAN_H

class Stickman{
public:
Stickman();
};
#endif

This is my source file:这是我的源文件:

#include "stickman.h"
#include <iostream>

using namespace std;

Stickman::Stickman(){
    cout << "Hello\n";
}

This is my main:这是我的主要内容:

#include "stickman.h"
#include <iostream>

int main(){
   Stickman figure;
}

This is the ERROR message in the terminal:这是终端中的错误消息:

Alexandres-MBP:Game alexandrecarqueja$ cd
"/Users/alexandrecarqueja/Desktop/Game/" && g++ main.cpp -o main && "/Users/alexandrecarqueja/Desktop/Game/"main
Undefined symbols for architecture x86_64:
"Stickman::Stickman()", referenced from:
 _main in main-d38641.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You need to call this instead:您需要改为调用它:

g++ main.cpp stickman.cpp -o main

which will also compile stickman.cpp .这也将编译stickman.cpp Then the linker will know what to do.然后链接器将知道该怎么做。 Right now you have a #include stickman.h in your main which declares the class, but does not define it.现在你的 main 中有一个#include stickman.h ,它声明了这个类,但没有定义它。

The linker sees a constructor is declared (in stickman.h ), but does not see how it is implemented ( stickman.cpp was not compiled).链接器看到声明了一个构造函数(在stickman.h ),但没有看到它是如何实现的( stickman.cpp没有被编译)。 Hence it is not able to link with the constructor body.因此它无法与构造函数体链接。

It must be compiler specific because I ran the code in Visual Studio and it built successfully.它必须是特定于编译器的,因为我在 Visual Studio 中运行了代码并成功构建了它。 I would suggest you get the free express/community Visual Studio 2017 IDE software if you happen to have a Windows computer.如果您碰巧有一台 Windows 计算机,我建议您获得免费的 express/community Visual Studio 2017 IDE 软件。 The code looks fine so I'm personally unsure of what may be causing your issue if it's not compiler related.代码看起来不错,所以我个人不确定是什么导致了你的问题,如果它与编译器无关。 If you only have a Mac computer, then I suggest maybe looking into other free compilers.如果您只有一台 Mac 计算机,那么我建议您可以查看其他免费编译器。

You also receive this error in vscode if your project has a path that includes spaces.如果您的项目的路径包含空格,您也会在 vscode 中收到此错误。 As mentioned above you also need to compile all your cpp-files.如上所述,您还需要编译所有 cpp 文件。

To do this in vscode in eg macOS Catalina please see my answer here https://stackoverflow.com/a/61331301/1071899要在例如 macOS Catalina 中的 vscode 中执行此操作,请在此处查看我的答案https://stackoverflow.com/a/61331301/1071899

Basically you need to make a tasks.json file with the compiler specific flags.基本上你需要用编译器特定的标志制作一个 tasks.json 文件。 Here you need to include that all *.cpp files should be compiled AND you need to escape the whitespaces by adding "\\"${workspaceFolder}\\"/*.cpp", instead of "${file}", .在这里,您需要包含应编译所有 *.cpp 文件,并且您需要通过添加"\\"${workspaceFolder}\\"/*.cpp",而不是"${file}",来转义空格。 Take note of the two \\" . This will make sure that your project path is surrounded by "" and it will not complain about linker errors.注意两个\\" 。这将确保您的项目路径被""包围,并且不会抱怨链接器错误。

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

相关问题 简单的c ++代码无法编译(链接器命令失败,退出代码为1) - simple c++ code does not compile (linker command failed with exit code 1) C ++链接器命令失败,退出代码为1 - C++ linker command failed with exit code 1 C ++链接器错误(“链接器命令失败,退出代码为1”) - C++ Linker Error (“Linker command failed with exit code 1”) 编译错误:链接器命令失败,退出代码为1 - Compile Error: linker command failed with exit code 1 Xcode链接器命令失败,退出代码为1 c ++ - Xcode linker command failed with exit code 1 c++ Xcode C ++错误“链接器命令失败,退出代码为1” - Xcode C++ Error “linker command failed with exit code 1” 链接器命令失败,退出代码为1 C ++ Xcode - linker command failed with exit code 1 C++ Xcode 启用 OpenMP 的简单代码无法编译:“clang: 错误:链接器命令失败,退出代码为 1” - Simple OpenMP-enabled code does not compile: "clang: error: linker command failed with exit code 1" 获取错误clang:错误:从终端编译C ++文件时,链接器命令失败,退出代码为1(使用-v查看调用) - Getting error clang: error: linker command failed with exit code 1 (use -v to see invocation) while compile C++ file from terminal C ++编译错误链接器命令失败 - c++ compile error linker command failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM