【发布时间】:2018-07-20 18:33:00
【问题描述】:
我有使用 notepad++ 用 C 编程编写的源文件,我从命令行运行它们,稍后我需要链接它们以生成 .exe 文件。
以下是我在生成 .exe 文件时要使用的以下命令
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
但是当我运行以下命令时,我的编译器返回以下错误状态。
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain@16'
当我运行以下命令来编译我的 mainc 文件时
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
这是我的 mainc.c 和 logc.c 以及 logc.h 文件供您参考
logc.c 文件在这里
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c 文件在这里
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
logc.h 文件在这里
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
如何编译单个源文件,然后链接它们并生成可执行文件。
提前致谢。
【问题讨论】:
-
与您的问题无关,但以下划线开头并后跟大写字母的符号(例如
_LOG_H)保留用于所有范围内的编译器和标准库实现。你不应该使用这样的符号。 -
编译时,始终启用警告,(对于
gcc,至少使用:-Wall -Wextra -Wconversion -pedantic -std=gnu11)也强烈建议使用参数:-ggdb,以便于使用@进行调试987654331@编译器
标签: c gcc command-line