首页 > 解决方案 > TCC 编译器:未定义符号“main”

问题描述

我正在尝试编译一个库,但不断收到这些错误。对C不太熟悉,也不知道如何解决这个问题。它不会创建 dll。

c:\>C:\tcc\tcc.exe C:\tcc\examples\hello_dll.c -o C:\tcc\examples\test_win.dll
tcc: error: undefined symbol 'hello_data'
tcc: error: undefined symbol 'hello_func'
//+---------------------------------------------------------------------------
//
//  HELLO_DLL.C - Windows DLL example - main application part
//

#include <windows.h>

void hello_func (void);
__declspec(dllimport) extern const char *hello_data;

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow)
{
hello_data = "Hello World!";
hello_func();
return 0;
}

标签: ctcc

解决方案


两个错误:

  1. 声明了 hello_data 变量,extern这意味着它没有在此程序模块中定义,因此需要来自其他地方,来自同时链接的其他地方。
  2. 您的 WinMain 例程调用 hello_func 但未定义。在 C 语言中,当您看到以它结尾的定义时,();这意味着这是一个原型,用于告诉编译器期望什么,而不是函数的实际代码。

我建议首先,您计划启动一个控制台类型的应用程序,例如臭名昭​​著的 hello world,例如:

#include <stdio.h>
int main()
{
    printf("Hello William\n");
}

这将在 MS Windows 下的 cmd 窗口中编译和运行。当你开始工作时,你可以考虑使用 Windows 环境和 DLL 中的 WinMain 之类的东西来做一些更有趣的事情。


推荐阅读