what's wrong in my C program? - c

a cpp file:
#include <iostream>
#include <jni.h>
#include "Hello.h"
#include "windows.h"
#include "stdafx.h"
typedef void(__stdcall *Print_)();
int main(){
HINSTANCE hDll; //DLL句柄
Print_ print_; //函数指针
hDll = LoadLibrary("Hello.dll");
if (hDll != NULL)
{
print_ = (Print_)GetProcAddress(hDll,"Java_Hello_sayHello#8");
if(print_!=NULL)
{
print_();
}
FreeLibrary(hDll);
}
return 0;
}
//there is something wrong, it prints:
http://i983.photobucket.com/albums/ae311/keatingWang/c_wrong.png
未声明的标识符 means : Undeclared identifier

Consider the macro:
#define HINSTANCE "hDll"
and its use:
HINSTANCE hDll; //DLL句柄
after preprocessing it would look like:
"hDll" hDll;
which clearly is an error as it makes hDll undeclared as "hDll" is not a valid type.

Could it be a pre-compiled header issue? With some project settings VC++ will skip stuff before the #include "stdafx.h", which I think might be the cause of the C4627 warnings you're getting. Have you tried moving #include "stdafx.h" before your other #includes?

remove
#define HINSTANCE "hDLL"
To remove C4627 warning, move up #include "stdafx.h" to the top (to be the first #include) as indicated by Mike Dinsdale's answer. This will probably solve error for LoadLibrary, GetProcAddress, and FreeLibrary:
#include "stdafx.h" // moved up
#include <iostream>
#include <jni.h>
#include "Hello.h"
#include "windows.h"

Related

Implicit declaration of 'memfd_create'

I'm trying to use functions from mman.h in my c code. The code compiles fine with g++/clang++, but when using gcc/clang it says that memfd_create has not been declared, however the code still runs fine.
I tried compiling online with godbolt and it's the same as locally, so I doubt it's something wrong with my setup. Does anyone know why this is happening? I'm using gcc 11.3 and clang 14.
#include <stdint.h>
#include <stdio.h>
#define _GNU_SOURCE
#include <sys/mman.h>
int main()
{
int32_t fd = memfd_create("", 0);
if (fd == -1)
{
printf("Error creating fd\n");
return -1;
}
return 0;
}
Compile warning:
main.c:9:15: warning: implicit declaration of function 'memfd_create' is invalid in C99 [-Wimplicit-function-declaration]
int32_t fd = memfd_create("", 0);
_GNU_SOURCE has to be before any #include. See man feature_test_macros.
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>

When I try and use Playsound() in c the compiler doesn't even compile it

So I've been trying to use this function called Playsound() but it just throws an error. the error is C:\Users\ETHANZ~1\AppData\Local\Temp\ctmDDBF.tmp:C:\Users\ETHANZ~1\AppData\Local\Temp\ctmDCC3.tmp:(.text+0xd): undefined reference to `PlaySoundA#12'
also here is my code
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
PlaySound("intro.mp3", NULL, SND_ASYNC);
}
If you are using MSVC as your compiler you can use a library pragma in your source.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib, "winmm")
int main()
{
PlaySound("intro.mp3", NULL, SND_ASYNC);
}

implicit declaration of pivot root causing a compiler error

I have been getting these compiler errors when I am trying to create a self-made containers
warning: implicit declaration of function ‘sys_pivot_root’; did you mean ‘SYS_pivot_root’? [-Wimplicit-function-declaration]
TRY (sys_pivot_root(wd, "/dir/oldroot"));
And then I change sys_pivot_root into SYS_pivot_root then the following error message appears.
install_rootg.c:61:9: error: called object is not a function or function pointer
TRY (SYS_pivot_root(wd, "/dir/oldroot"));
and then I look into syscall.h to see if the function exists. I get the following line
asmlinkage long sys_pivot_root (const char __user * new_root, const char __user * put_old)
why am I getting these compiler errors? I haven't been able to resolve this for like a week now.
I include the header files in this exact order.
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <limits.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/mman.h>
any help would be appreciated. Thank you in advance!
I figured out, so there isn't such function already defined as pivot_root.
you can just call syscall(SYS_pivot_root, ...
and pivot_root is called.
look at the man page for the usage.

C macro name must be an identifier

I've created a c project and this is the beginning of the main.c file:
#include <curl/curl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "include/httpdef.h"
//...some code
The httpdef.h beginning is this:
#ifndef httpdef
#define httpdef
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
//definitions
#endif
At the very first line of both files I get the error from the gcc compiler:
macro name must be an identifier
What could be the problem?
EDIT: I realized now that actually the compiler doesn't give any error, it's my vim plugin (YouCOmpleteMe) that generates this error. If I compile everything works and the error doesn't appear

Im getting the error: expected '=', ',', ';', 'asm', or ''__attribute__' before 'void'

I am experiencing this error at my preprocessText() function (below) in my .c and I'm not entirely sure why. From browsing it seems most people were missing a { or ( or ; etc somewhere, but I'm fairly certain I am not.
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "Assembler.h"
int main(int argc, char** argv) {
// ...
preprocessText(file, inter1);
// ...
}
public void preprocessText(FILE* file, FILE* file2) { //error happens at this declaration
// ...
}
My header file is:
#ifndef ASSEMBLER_H
#define ASSEMBLER_H
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
// ...
void preprocessText(FILE* file, FILE* file2);
#endif
All methods are implicitly accessible by any other piece of code, if the function name is in scope. There is no public keyword in c
You have 'public' before 'void'. Remember, this is C ;)

Resources