繁体   English   中英

在 macOS Catalina 上使用 clang++ 编译头文件和 cpp 文件的问题

[英]Problem with compiling header and cpp file on macOS Catalina in clang++

我一直在尝试运行此代码,但我一直收到这么长的错误消息。 我认为代码没问题,因为它在我的 Windows 计算机上运行,​​所以我认为这是我的编译器的问题。 我在 macOS Catalina 10.15.7 和 clang 12.0.0 上使用 VisualStudioCode。

  1. 这是我的 main.cpp 文件
#include<iostream>
#include <memory>
#include "Wezel.h"

using namespace std;

int main(){
    for (int i = 0; i < 10; i++) {
    unique_ptr<Wezel>(new Wezel()); 
    }
    return 0;
}    
  1. Wezel.cpp 文件
#include "Wezel.h"
#include <iostream>

using namespace std;

Wezel::~Wezel() {
  cout << "Destruct" << endl;
}
  1. Wezel.h 文件
#ifndef WEZEL_H
#define WEZEL_H

class Wezel
{
    public:
    ~Wezel();
};

#endif
  1. 尝试编译 main.cpp 文件时收到的错误消息
majkel@mbp-micha RecyclingCpp % cd "/Users/majkel/Documents/Obiektowe/RecyclingCpp/" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "/Users/majkel/Documents/Obiektowe/RecyclingCpp/"tempCodeRunnerFile
Undefined symbols for architecture x86_64:
  "Wezel::~Wezel()", referenced from:
      std::__1::default_delete<Wezel>::operator()(Wezel*) const in tempCodeRunnerFile-40bbc5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

您没有告诉编译器Wezel.cpp是您尝试构建的内容的一部分,因此它不知道在哪里可以找到Wezel::~Wezel()

像这样尝试:

g++ main.cpp Wezel.cpp

暂无
暂无

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

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