【问题标题】:Visual Studio (2015) C Project dependency - LNK1561 entry point must be definedVisual Studio (2015) C 项目依赖 - 必须定义 LNK1561 入口点
【发布时间】:2015-12-03 08:39:11
【问题描述】:

我在一个 C 项目中有以下主程序,它(这个想法)在 same 解决方案中使用 another 项目的头文件和 utils 文件。头文件包含 utils 文件中的函数声明(如下所示)

主要功能:

#include <stdio.h>
#include "header1C.h"
#include "utils_1C.c"
#include <stdbool.h>

int main() {
    int num = 0, base = 0, rKey; 
    do {
        bool chk = false; //flag for while loop to check if base is correct
        printf("\nEnter number [Enter -1 to exit]: ");
        scanf_s("%d", &num); //number to be changed
        if (num != -1) {
            do {
                printf("Enter new base [2,4,8 or 16]: ");
                scanf_s("%d", &base); //new base [2,4,8,16]
             if ((base != 2) && (base != 4) && (base != 8) && (base != 16)){                  
                printf("Number must be either 2,4,8 or 16\n");
                }
                else chk = true; //set flag to true
            } while (chk == false);
            newBase(num, base); //calls newBase() with parameters num and base
        }
        rKey = getchar(); //getchar to clear char buffer
        if (num == -1)
            printf("\n[Press any key to exit]");
        rKey = getchar(); //readkey to exit programm on any key press
    } while (num != -1); //to be repeated until num entered by user is -1
return 0;
}

在一个名为 Library 的单独项目中:

头文件 header1C.h:

char checkHex(int n);

void newBase(int n, int b);

实用程序文件 utils_1C.c:

#include "header1C.h"

char checkHex(int n) { //used by newBase() - converts given number to letters if larger than 9
    switch (n) {
    case 10: return 'A';  //return A if n = 10
    case 11: return 'B';  //return B if n = 11
    case 12: return 'C';  //return C if n = 12
    case 13: return 'D';  //return D if n = 13
    case 14: return 'E';  //return E if n = 14
    case 15: return 'F';  //return F if n = 15
    }
    return n; //return given number if < 10
}

void newBase(int n, int b) {

    static char newNum[50];       //array in which the number is stored (array of remainders)
    int i = 0;                    //counter
    do {                          //long division and gathering of remainder
        newNum[i] = checkHex(n % b);  //calling checkHex() to check number and conert to letter if needed
        n = n / b; //divide n by b
        i++; //counter + 1
    } while (n >= b); //loop stops when n is smaller than it's divider b
    newNum[i] = checkHex(n); //calling checkHex()

    printf("\nNumber in base %d: ", b); //flipped number - proper answer
    for (i; i >= 0; i--) { //loop stops when smaller than 0
        if ((newNum[i] >= 65) && (newNum[i] <= 70))  //check if newNum[i] is a letter [A-F]
            printf("%c", newNum[i]);  //output if letter
        else
            printf("%d", newNum[i]);  //output if number
    }
}

我收到此错误:

错误 LNK1561 入口点必须是 定义库 C:\Users...\Assignments\Library\LINK 1

我已将库作为项目依赖添加到具有主要功能的项目中,并且我也玩过

属性 -> 配置属性 -> 链接器 -> 系统 -> 子系统

按照我在 Stack Overflow 上找到的其他答案,但没有运气。

此时我不知道该怎么办,坦率地说,我没想到会出现这么大的问题(尽管我可能遗漏了什么?)

对于这个冗长的问题,我们深表歉意,非常感谢您的帮助。

【问题讨论】:

  • main() 返回 int 而不是 void
  • @alk 我故意使用 void 因为我不需要它来返回任何东西,尽管我不认为这是这里的问题。
  • 你是用 C 还是 C++ 编译器编译这个?
  • 它们是 .c 文件,所以我猜是 C 编译器,尽管 Visual Studio 同时支持 C 和 C++,所以这不是问题
  • 所以你似乎使用 C++,那么 void main() 肯定是问题所在,因为在 C++ 中 int main()void main() 是不一样的。

标签: c++ c visual-studio-2015


【解决方案1】:

使用 C++ 编译代码时,链接器需要 int main()

当您将main() 定义为返回void 时,链接器找不到程序的入口点。

C++ 确实知道函数的返回类型、函数的签名,而不是 C。

【讨论】:

  • 将主函数更改为 int 返回 0,从头开始重建整个解决方案 - 错误仍然存​​在
  • 您确实创建了一个控制台程序项目而不是 Windows 程序项目? @Logan
  • 如果做 C++ 至少应该是 int main(void) 例如。 @Logan
猜你喜欢
  • 1970-01-01
  • 2013-09-11
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多