realpath throws undefined reference to realpath - c

Im trying to get the path of text file , when i use the method "realpath" & #include<stdlib.h> ,the compiler gives me an error message :"undefined reference to realpath"

realpath doesn't exist on Windows, which isn't fully POSIX compliant.
On Windows you can try to define it this:
#include <stdlib.h>
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
I know this works with MinGW-w64, but it should work with MSVC too.
If you're writing portable code you can just put this somewhere at the top to keep your code working for multiple platforms:
#ifdef _WIN32
#include <stdlib.h>
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#endif

Related

cannot find -lws_32.lib while compiling C program

I use Windows and codeblocks 13.12 IDE. I'm writing a C program need functions in <Winsock2.h> (like "WSAStartup" function) ; because Logs showed "Undefined Reference to WSAStartup", I included the command below according to the article WSAStartup link error.
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
in case compiling process went wrong while linking library, I had put my program in a project and added "ws2_32.lib" into list of "Link libraries"; however, Logs only shows "cannot find -lws2_32.lib" I'm confused because I have checked that "ws2_32.dll" file exists in my C:\Windows\System32. How should I resolve the problem?

Troubles with VSCode and Windows for a simple C code

I am trying to use VSCode for writing and executing C codes for a course in Windows 10. I installed VSCode and MinGW as the instructions said. I'm trying to run a simple code (print "Hello world"), but when I run the code, the output says "Access denied"
//Test code for C in Windows 10
#include "stdio.h"
#include "stdlib.h"
void main(){
printf("Hello world");
}
I'm not sure if it's gonna solve your problem but when you include header from LibC or any different lib you must use this syntax
#include <stdio.h>
#include <stdlib.h>
If you use < symbol, the preprocessor will look in special path defined by your environement else if you use " symbol, the preprocessor will look in your current directory,

TURBO C++: Unable to open include file stdio.h

I am trying to compile a simple C program using TUrbo C++ 3.2. But getting the following error: Unable to open include file 'STDIO.h'
I do have these files in INCLUDE library.
Cant help you if you dont post your code. Check if you use #include <cstdio> (not #include "cstdio" or #include <cstdio.h> or #include "cstdio.h".
#include <cstdio> will always work.

Windows api programming with c: undefined reference to OpenJobObject

i am trying to write a short program which allows me to terminate a windows job object by its name. Here is the (shortend) code of file TerminateJobObject.c:
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#endif
#define JOB_OBJECT_TERMINATE 0x0008
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <limits.h>
LPTSTR jobObjectName;
HANDLE jobObj;
int main(int argc, TCHAR *argv[]){
jobObjectName = argv[0];
jobObj = OpenJobObject(JOB_OBJECT_TERMINATE,FALSE,jobObjectName);
TerminateJobObject(jobObj,0);
}
I get the following error when compiling with "gcc TerminateJobObject.c -o TerminateJobObject":
TerminateJobObject.c: In function 'main'
C:/<...>:TerminateJobObject.c:(.text+0x62):undefined reference to 'OpenJobObject'
collect2: ld returned 1 exit status
I don't understand why the linker can't resolve OpenJobObject. TerminateJobObject is linked correctly and is also from the windows api.
What i tried so far:
Compiler: gcc, clang
Different versions for _WIN32_WINNT (0x500,0x600,0x601)
Different OS: Windows 7 and Windows server 2008
"OpenJobObjectW" and "OpenJobObjectA"
Defining WINVER
I am not very experienced with c and windows api and can't find anything on this problem, so it would be great if somebody could point me in a direction.
Api reference: OpenJobObject
I solved it. The Problem was/is that the header file winbase.h of mingw32 (which is included via windows.h) is missing the function definition for OpenJobObject as Harry Johnston suspected.
I added the following lines to .../mingw/include/winbase.h
#define OpenJobObject __MINGW_NAME_AW(OpenJobObject)
WINBASEAPI HANDLE WINAPI OpenJobObjectA (DWORD dwDesiredAccess, WINBOOL bInheritHandle, LPCSTR lpName);
WINBASEAPI HANDLE WINAPI OpenJobObjectW (DWORD dwDesiredAccess, WINBOOL bInheritHandle, LPCWSTR lpName);
Which i found in the winbase.h from mingw-w64
Next I changed the call to "OpenJobObjectA" and now it works. :)
Thanks for the help!
Edit: As Hans Passant pointed out a cleaner way would probably be to migrate to mingw-64 or something else.

how to use crypt( ) method in Linux?

I just want to use crypt() to generate an encrypted password,and I write a demo which invoke the crypt() method.
Here is my code
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%s\n",crypt("abc","ab"));
exit(0);
}
I compile it using "gcc tem.c -lcrypt' and when I run it, everything seems right, but a "segment error" shows up. so please tell me what's wrong with this simple program?
If you compile with the flag -Wall you will see why.
If you read the manual page you will see that it uses #define _XOPEN_SOURCE before including <unistd.h>. It should actually be defined before including any header.
If you don't define _XOPEN_SOURCE then the crypt function will not be prototyped. Then the compiler doesn't know what the actual return type is, or the types and number of arguments. So it will assume that the function returns an int and your printf expects a string, so there will be a type mismatch that causes the crash.
You need this:
#define _XOPEN_SOURCE
at the top of your source file, before any #include.
Alternatively compile with the gcc option -D_XOPEN_SOURCE.
Looks like it could be related to crypto library support.
Try adding:
#include <crypt.h>
[mstanislav#pardalislabs ~]$ gcc tem.c -lcrypt
[mstanislav#pardalislabs ~]$ ./a.out
abFZSxKKdq5s6
Looks good for me!

Resources