簡體   English   中英

CPP從另一個.cpp文件調用函數

[英]CPP Calling a function from another .cpp file

我有3個檔案。

main.cpp

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

using namespace std;

int main() {
    abc();

    return 0;
}

第二個是function.h

#pragma once
#include <iostream>

void abc();

最后一個是function.cpp

#include "function.h"

void abc() {
    std::cout << "ABC!" << std::endl;
}

當我嘗試編譯它時,我得到了abc()鏈接器錯誤的多個定義。 如何解決? 如何從另一個文件執行功能?

您應該在main.cpp包含function.cpp 抱歉,我之前的回答是錯誤的。

請使用g++ main.cpp function.cpp編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM