android - 在c编程中包含在android studio中不起作用的file.h(使用SDL2)

标签 android c android-studio include linker-errors

我实际上正在使用 SDL 2(一个 C 图形库)在 Android Studio 上开发一个 C 应用程序。但问题并非来自库本身,而是来 self 自己文件的 #include

我创建了源文件,在其中放置了我的函数,并将它们与函数声明的头文件关联起来。但是当我调用主源文件中的函数时,出现错误: 错误:例如,未定义对“draw_render_rectangle”的引用。 它不适用于我的任何文件或功能。但它适用于 SDL。

所以我尝试#include。 主源文件“main_graphics.c”:

#include "main_graphics.h"
void disp_main_menu(int selected) {
    POINT A, B;
    A.x = 0;
    A.y = 0;
    B.x = WIDTH;
    B.y = HEIGHT;
    draw_render_rectangle(A, B, 70, 70, 70);
}

主头文件“main_graphics.h”:

#ifndef ANDROID_GAME_MAIN_GRAPHICS_H
#define ANDROID_GAME_MAIN_GRAPHICS_H


#include "../SDL2/include/SDL.h"
#include "../SDL2/include/SDL.h"
#include "../SDL2_ttf/SDL_ttf.h"
#include "../SDL2_image/SDL_image.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "graphics_mobile.h"
int WIDTH = 1080;
int HEIGHT = 1920;

void disp_main_menu(int selected);

#endif //ANDROID_GAME_MAIN_GRAPHICS_H

“graphics_mobile.c”:

#include "graphics_mobile.h"

typedef struct point {int x,y;} POINT;

void draw_render_rectangle(POINT p1, POINT p2, Uint8 r, Uint8 g, Uint8 b)
{
    SDL_Rect rectangle;
    int xmin, xmax;
    int ymin, ymax;
    int i,j;

    if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
    if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}

    rectangle.x = xmin;
    rectangle.y = HEIGHT - (ymax-ymin) - ymin;
    rectangle.w = xmax-xmin;
    rectangle.h = ymax-ymin;
    SDL_SetRenderTarget(renderer, texture);
    SDL_RenderDrawRect(renderer,&rectangle);
    SDL_SetRenderDrawColor(renderer, r, g, b, 0x00);
    SDL_RenderFillRect(renderer, &rectangle);
    SDL_SetRenderTarget(renderer, NULL);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
}

“graphics_mobile.h”

#ifndef ANDROID_GAME_GRAPHICS_MOBILE_H
#define ANDROID_GAME_GRAPHICS_MOBILE_H
#include "main_graphics.h"

typedef struct point {int x,y;} POINT;

void draw_render_rectangle(POINT p1, POINT p2, Uint8 r, Uint8 g, Uint8 b);

#endif //ANDROID_GAME_GRAPHICS_MOBILE_H

奇怪的是,自动补全功能正在工作,当我输入该函数时,它实际上识别了它,并在自动补全的小窗口中告诉我它是在“graphics_mobile.h”中定义的。

问题可能出在编译器上吗? 我尝试了 ndk-bundle_10e 和 ndk-bundle_16b,但得到了同样的错误。

最佳答案

所以我做了一些测试,得到了一个有效的代码:

您必须输入“graphics_mobile.h”:

#ifndef ANDROID_GAME_GRAPHICS_MOBILE_H
#define ANDROID_GAME_GRAPHICS_MOBILE_H
#include "main_graphics.h"
#include "graphics_mobile.c"

#endif //ANDROID_GAME_GRAPHICS_MOBILE_H

并删除函数的声明

关于android - 在c编程中包含在android studio中不起作用的file.h(使用SDL2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56056735/

相关文章:

java - 线程进度条 fragment

android - 打开软键盘时,Android Recyclerview Java.lang.NullPointerException

c - 使用 valgrind 时大小为 1 的读取无效

android - 如何移动 ANDROID_SDK_HOME

java - 在 RecyclerView 中显示列表

android - 使用来自 Android 的 Cognito 凭证在 AWS API Gateway 上调用 API

android - 有关下载文件的元数据存储在哪里?

c - 来自文件的字符串数组的段错误

C 字符 ** 导致内存泄漏

java - 在每次仪器测试之前重置/清除应用程序 (Android Studio)