gcc 在Visual Studio中使用外部头文件运行C脚本时出现问题

2vuwiymt  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(189)

我正在尝试解决cs50课程中的一个问题集,然而,当我尝试在我的桌面版本的Visual Studio Code上运行以下代码时,我遇到了编译错误。

#include <stdio.h>
#include <string.h>
#include "cs50.h"

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

    int voter_count = get_int("Number of voters: ");
    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }
    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    // TODO
    for (int i=0;i<candidate_count;i++){
        if(!strcmp(name,candidates[i].name)){
            candidates[i].votes +=1;
            return true;
        }
    }

    return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO
    int max=0;
    string max_name = "";
    for (int i=0;i<candidate_count;i++){
        if(max<candidates[i].votes){
            max = candidates[i].votes;
            strcpy(max_name,candidates[i].name);
        }
    }
    for (int i=0; i<candidate_count;i++){
        if(candidates[i].votes==max){
            printf("%s\n",candidates[i].name);
        }
    }
    return;
}

终端出现以下错误;

*  Executing task: C/C++: gcc.exe build active file 

Starting build...
C:/msys64/mingw64/bin/gcc.exe -fdiagnostics-color=always -g D:\Work\C-Language\Plurality.c -o D:\Work\C-Language\Plurality.exe 
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Admin\AppData\Local\Temp\ccrzNxos.o: in function `main':
D:/Work/C-Language/Plurality.c:49: undefined reference to `get_int'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Work/C-Language/Plurality.c:53: undefined reference to `get_string'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

我已经使用以下链接设置了我的Visual Studio Code for C/C++; https://code.visualstudio.com/docs/languages/cpp
终端显示程序无法同时编译外部头文件,您能否指导我完成此操作,因为这是我第一次使用C/C的VS代码,之前我曾在Dev C上工作,如果有一些特定的配置:如果您想建议在C/C++程序中使用VS代码,我们将不胜感激。

  • 谢谢-谢谢
soat7uwm

soat7uwm1#

将cs50库链接添加到编译命令中。如何操作将在第2周的笔记和视频中解释。第一个主题是关于编译的。
此外,当您在尝试编译时输入命令时,而不仅仅是输出时,将其置于引号中会提供更多信息

相关问题