簡體   English   中英

使用CodeBlocks IDE在C中使用未定義的引用錯誤

[英]Undefined reference error in C using CodeBlocks IDE

我只是在玩代碼塊並編寫一個非常簡單的程序,但是我得到了未定義的引用錯誤...我將完全相同的代碼復制到其他地方並在終端中編譯它並且工作正常但是只是不起作用在代碼塊中。

我的main.c是:

#include <stdio.h>
#include "subfunc.h"

int main()
{
    printhello();
    return 0;
}

然后我的subfunc.c是:

#include <stdio.h>
#include "subfunc.h"

void printhello(void)
{
    printf("hello world in function\n");
}

然后我的subfunc.h是:

void printhello(void);

謝謝

UPDATE

其實......我明白了。 我右鍵單擊文件subfunc.c和subfunc.h並選擇屬性並轉到構建選項卡,我發現編譯文件和鏈接文件的選項未勾選。 謝謝

在此輸入圖像描述
請參見上圖並與您的項目進行比較。 僅與紅色圓圈比較。 忽略剩下的(剩下的是我的項目/作業)在my_subfunction.h中寫

    #ifndef __MY_SUBFUNC_H__
    #define __MY_SUBFUNC_H__


    #include<stdio.h>
    void printhello(void);
    #endif

在main.c

#include <stdio.h>
#include "my_subfunc.h"

int main()
{
    printhello();
    return 0;
}

和my_subfun.c: -

#include <stdio.h>
#include "my_subfunc.h"

void printhello(void)
{
    printf("hello world in function\n");
}

我希望這會有所幫助..因為這對我有用。

你沒有確切地說明未定義的錯誤是什么,但我認為它與你的main()printhello()函數有關。

確保您編譯你的main.c和subfunc.c后,聯系兩者的目標文件main.obj和subfunc.obj在您的項目。 這應該自動發生在代碼塊中,假設您為正在構建的當前項目包含main.c和subfunc.c。

暫無
暫無

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

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