안드로이드 용 lwIP를 이식하고 유닉스 용 포트에서 일부 코드를 추출했는데 지금 다음 논리를 컴파일 할 수없는 이유를 모르겠습니다. .struct **와 C. struct * in dereference와 address-of 연산자의 혼동
유닉스 용 포트에 사용 된 기능은 #define
및 typedef
입니다. 나는
#ifndef FUNC_H
#define FUNC_H
#include <stdint.h>
struct sys_sem {
unsigned int c;
};
#define sys_mutex_t sys_sem_t;
typedef struct sys_sem * sys_sem_t;
void sys_arch_sem_wait(struct sys_sem **s, int timeout);
#endif
나는 또한, 자신의 #define
의 복사 func.h의 코드를 작성하고 sys.h
#ifndef SYS_H
#define SYS_H
#define sys_mutex_lock(mutex) sys_sem_wait(mutex)
#define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0)
#endif
로 붙여 넣기 그리고 내 기능은 내가 자바에서 호출이 기계적 인조 인간; 그것은 com_sample_MainActivity.c
#include "func.h"
#include "sys.h"
static sys_mutex_t
mem_mutex;
JNIEXPORT jstring JNICALL Java_com_sample_MainActivity_messageFromNativeCode(
JNIEnv *env, jobject thisObj) {
sys_mutex_lock(&mem_mutex);
sys_mutex_lock(121); // Even this can be compiled
return (*env)->NewStringUTF(env, "Hello from native code!");
}
sys_mutex_t
이 sys_sem_t
의 동의어 파일에, 그리고 sys_sem_t
는 sys_sem *
의 동의어입니다. 내가 맞습니까? 내 서명 sys_arch_sem_wait
은 struct sys_sem **s. However, I pass the address of
sys_mutex_t which after all the
#define s means the same as passing a
sys_sem`을 수락하고 코드가 컴파일됩니다.
에도이 나는
sys_mutex_lock(121); // Even this can be compiled. Why?
그래서 sys_mutex_lock
아무것도 전달할 수
내 테스트.
01-07 14:46:40.695: D/Sample(22086): s != NULL
01-07 14:46:40.695: D/Sample(22086): *s == NULL
예상되는 동작 : 여기
#include "func.h"
#include <android/log.h>
void sys_arch_sem_wait(struct sys_sem **s, int timeout) {
if (s == NULL) {
__android_log_print(ANDROID_LOG_DEBUG, "Sample", "s == NULL");
} else {
__android_log_print(ANDROID_LOG_DEBUG, "Sample", "s != NULL");
}
if (*s == NULL) {
__android_log_print(ANDROID_LOG_DEBUG, "Sample", "*s == NULL"); // It causes the failure
} else {
__android_log_print(ANDROID_LOG_DEBUG, "Sample", "*s != NULL");
}
}
그리고는 로그 캣의 출력입니다.
나는이 ...
sys_mutex_lock(<arg>);
가 ...에 해당,
void sys_arch_sem_wait(struct sys_sem **s, int timeout)
가 다른 서명 제시 매크로 감안할 때
void sys_arch_sem_wait(struct sys_sem *s, int timeout)
경고없이 컴파일 되었습니까? –
@SouravGhosh, 타입 비호 환성에 대한 두 가지 경고가 있습니다 : 경고 : 호환되지 않는 포인터 타입 [기본값으로 활성화 됨]에서 'sys_arch_sem_wait'의 인수 1 전달'및 'sys_arch_sem_wait'의 경고 : 전달 인자 1은 캐스트없이 정수에서 포인터를 만듭니다. [기본적으로 사용 설정 됨] ' –
그리고 그 다음에 _ignore_를 선택 했습니까? 왜? 그들은 웬일인지 거기에있다? 그것들은 아주 자명하다. 컴파일러를 비난하지 마십시오. 그것은 자신의 직업이었습니다. 우리는 그 중 하나입니다. :-) –