GCC Linux C - 编译 object header 未找到
GCC Linux C - Compile object header not found
我一直在使用 C 语言(在 Linux 下)开发一个需要另一个模块(header 位于其他目录中)的模块。
我的问题是,当我使用 Makefile 编译代码时,gcc 编译器告诉我找不到某些 header。
gcc -c render.c
所以我包括目录以找到 header 但是在这里,gcc 试图找到不存在的 "main" 函数:它是一个模块...
gcc /opt/vc/include -c render.c
所以我想知道如何编译一个需要其他模块的模块(在module.o中输出)?
这是我的文件:
render.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "render.h"
int width,height;
int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void))
{
init(&width, &height);
loop = -1;
counter = 0;
setup();
while(loop==-1)
{
Start(width, height);
draw();
End();
counter++;
}
end();
finish();
exit(0);
return 0;
}
render.h:
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
#ifndef RENDER_H_
#define RENDER_H_
extern int width,height;
extern int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void));
#endif
生成文件:
INCLUDEFLAGS=-I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -IopenVG
LIBFLAGS=-L/opt/vc/lib -lGLESv2 -lEGL -lbcm_host -lpthread -ljpeg -LopenVG
NEEDED= openVG/libshapes.o openVG/oglinit.o
all: render
render.o: render.c
gcc -Wall -g $(INCLUDEFLAGS) -c render.c
你可能想要
gcc -Wall -g -I/opt/vc/include -c render.c
这将生成一个 render.o
目标文件。
请花点时间阅读关于invoking GCC的文档。特别是检查每个选项 -Wall
、-g
、-I
和 -c
的含义。恕我直言,前两个 非常重要。
稍后,您可能希望将所有目标文件 link 转换为可执行文件,并带有一些外部库。也许你想要像
这样的东西
gcc -g -Wall -L/opt/vc/lib render.o main.o -lvc -o myprogram
(你 真的想要 -Wall
和 -g
选项;恕我直言,你需要成为专家才能敢于避免它们;一旦你调试了你的程序并想要对其进行基准测试,添加 -O2
进行优化)
但当然,您需要其他选择。
请注意 gcc
的参数顺序很重要很多
当然,您应该了解GNU make and you need to use it. See this and that examples. You might use make --trace
(with recent make
) or remake 来调试您的Makefile
(这不好)。您还应该 运行 一次 make -p
以了解更多 make
.
的内置规则
也许你想要一个图书馆,然后阅读Program Library HowTo。
我一直在使用 C 语言(在 Linux 下)开发一个需要另一个模块(header 位于其他目录中)的模块。 我的问题是,当我使用 Makefile 编译代码时,gcc 编译器告诉我找不到某些 header。
gcc -c render.c
所以我包括目录以找到 header 但是在这里,gcc 试图找到不存在的 "main" 函数:它是一个模块...
gcc /opt/vc/include -c render.c
所以我想知道如何编译一个需要其他模块的模块(在module.o中输出)?
这是我的文件:
render.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "render.h"
int width,height;
int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void))
{
init(&width, &height);
loop = -1;
counter = 0;
setup();
while(loop==-1)
{
Start(width, height);
draw();
End();
counter++;
}
end();
finish();
exit(0);
return 0;
}
render.h:
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
#ifndef RENDER_H_
#define RENDER_H_
extern int width,height;
extern int loop,counter;
int initRender(void(*setup)(void),void(*draw)(void),void(*end)(void));
#endif
生成文件:
INCLUDEFLAGS=-I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -IopenVG
LIBFLAGS=-L/opt/vc/lib -lGLESv2 -lEGL -lbcm_host -lpthread -ljpeg -LopenVG
NEEDED= openVG/libshapes.o openVG/oglinit.o
all: render
render.o: render.c
gcc -Wall -g $(INCLUDEFLAGS) -c render.c
你可能想要
gcc -Wall -g -I/opt/vc/include -c render.c
这将生成一个 render.o
目标文件。
请花点时间阅读关于invoking GCC的文档。特别是检查每个选项 -Wall
、-g
、-I
和 -c
的含义。恕我直言,前两个 非常重要。
稍后,您可能希望将所有目标文件 link 转换为可执行文件,并带有一些外部库。也许你想要像
这样的东西 gcc -g -Wall -L/opt/vc/lib render.o main.o -lvc -o myprogram
(你 真的想要 -Wall
和 -g
选项;恕我直言,你需要成为专家才能敢于避免它们;一旦你调试了你的程序并想要对其进行基准测试,添加 -O2
进行优化)
但当然,您需要其他选择。
请注意 gcc
的参数顺序很重要很多
当然,您应该了解GNU make and you need to use it. See this and that examples. You might use make --trace
(with recent make
) or remake 来调试您的Makefile
(这不好)。您还应该 运行 一次 make -p
以了解更多 make
.
也许你想要一个图书馆,然后阅读Program Library HowTo。