Windows api programming with c: undefined reference to OpenJobObject - c

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.

Related

realpath throws undefined reference to realpath

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

Why no <string.h> in getopt library?

I have been using Ludvig Jerabek's port of the GNU getopt on Windows and getting errors on lines like:
if (d->optind != argc && !_tcscmp(argv[d->optind], _T("--")))
_tcscmp is a macro that resolves to strcmp on my system and then it reports strcmp not found. If we examine the headers in getopt.cpp:
/* Getopt for Microsoft C
....
Date: 02/03/2011 - Ludvik Jerabek - Initial Release
....
Revisions:
....
08/09/2011
....
#include <stdlib.h>
#include <stdio.h>
#include "getopt.h"
We see the problem: <string.h> is not included. I guess in Visual Studio <string.h> is included automatically maybe? I know I have successfully built getopt.cpp in Visual Studio, but using a manual environment with gcc on MinGW it is complaining about all the string compare functions being missing. What is the explanation for this?
The real tchar.h causes either #include <string.h> or #include <mbstring.h> depending on the character-set macros.
You have a broken version of tchar.h that does not correctly emulate these Microsoft-specific "Generic-Text Routine Mappings".

sockets.h problem with cygwin and eclipse

I keep getting this error:
c:\cygwin\usr\include/cygwin/socket.h:55:3: error: expected specifier-qualifier-list before '__uid32_t'
My code is mininmal and still can't compile:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
I have include C:\cygwin\usr\include to compiler path and linker to C:\cygwin\usr\lib
please advise
Headers under C:\cygwin\usr\include, and libraries under C:\cygwin\usr\lib, are for use in the Cygwin environment -- which means they should be referred to via their Cygwin paths, /usr/include and /usr/lib.
I haven't used mingw, but I don't think it's part of Cygwin, and I wouldn't expect it to be able to use Cygwin's headers and libraries.
If you're going to use mingw, you should use the headers and libraries that are installed with it. Don't specify any Cygwin-related directories.
Or you can use Cygwin's gcc (which is optional; you'll have to install it with setup.exe). In that case, you can run gcc or make from the command line. I'm not sure whether Cygwin and Eclipse play nicely together.
I know this question is old, and I'm using a newer version, but I had a similar issue and it was resolved by moving #include <sys/socket.h> one line above #include <sys/types.h>.
Something in types.h appears to be blocking socket.h (not sure how). Wish I had more details on the cause, but hope the solution helps some.

GetCurrentHwProfile in Code::Blocks (C)

I am trying to get the GUID from windows in C in the Code::Blocks IDE with the following code:
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
void getHWID()
{
HW_PROFILE_INFO hwProfileInfo;
if(GetCurrentHwProfile(&hwProfileInfo))
{
printf("Hardware GUID: %s\n", hwProfileInfo.szHwProfileGuid);
printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
}
}
I keep getting these errors even though I am linking and including all the files required:
In function 'getHWID': warning:
implicit declaration of function
'GetCurrentHwProfile'
In function getHWID': undefined
reference toGetCurrentHwProfile'
||=== Build finished: 1 errors, 1
warnings ===|
Let me know if anyone has had this problem or know how to fix it. Also if I right click HW_PROFILE_INFO or GetCurrentHwProfile and click Find Declaration it says not found.
I would like to get this to work, but I am also open to other simple ways to get this done.
edit: I've included Winbase.h now and it found a declaration for HW_PROFILE_INFO but I still get a undefined reference error for GetCurrentHwProfile
Have you configured Code::Blocks to include the correct SDK (I believe this function is part of the Windows SDK)? I suggest using Microsoft Visual Studio to write Windows code.
EDIT: I'm not sure if this is all you need to do, but there is a section in the their wiki about how to use the Microsoft's compiler.
Change
GetCurrentHwProfile
to
GetCurrentHwProfileA
or add
#ifdef UNICODE
#define GetCurrentHwProfile GetCurrentHwProfileW
#else
#define GetCurrentHwProfile GetCurrentHwProfileA
#endif
have a look GetCurrentHwProfile was not declared in this scope using MinGW's g++ compiler

Why multiple definitions? Why are other references not defined? This is really basic, what am I missing?

I have a small project that I need to compile. I have one header and one source that I have created and a nearly empty driver.c that includes my header.
Observe:
// iol.h
#ifndef __IOL_HEADER
#define __IOL_HEADER
/* program: iol.h
date: 5 October 2010
*/
#define UNIX 1
#define WINDOWS 2
#define OS UNIX
#if OS == UNIX
#include <ncurses.h>
#elif OS == WINDOWS
#include <conio.h>
#include <windows.h>
// Function declarations!
#endif
void iol_init(void);
#endif
Now my implementation file:
// iol.c
#include <string.h>
#include <stdlib.h>
#include "iol.h"
void iol_init(void) {
#if OS == WINDOWS
/* no startup required for windows */
#elif OS == UNIX
initscr();
noecho();
cbreak();
keypad(stdscr, 1);
// Implmntn continues....
Now the driver that includes my header and provides the main ():
//main.c
#include "iol.h"
My bash command:
gcc iol.c driver.c -l"ncurses"
I get back:
/tmp/ccmmW6hQ.o:iol.c:(.text+0x83f): first defined here
/tmp/ccwIKUaT.o: In function 'isEscaping':
driver.c:(.text+0xbab): multiple definition of 'isEscaping'
/tmp/ccmmW6hQ.o:iol.c:(.text+0xbab): first defined here
/tmp/ccwIKUaT.o: In function 'initSeq':
..
driver.c:(.text+0x149): undefined reference to 'iol_prnstr'
driver.c:(.text+0x178): undefined reference to 'iol_putch'
..
driver.c:(.text+0x726): undefined reference to 'iol_display'
collect2: ld returned 1 exit status
I just want to get to point where I can compile this, and start ripping my hair out 'cuz of all my seg-faults. What's the problem in my setup? I RTFM on the Gnu C Compiler apparently I'm doing what I'm supposed to, which is declare stuff in iol.h, define in iol.c, and use it in driver.c this is pretty trivial stuff maybe I just need a second set of eyes :S
I'm actually getting a long list of errors, if anyone thinks that's relevant, I'm happy to post the whole source.
this is the linker complaining. This is what you would get if you had a function defined in the header file that was not declared 'inline'
the missing ones are because you have not added the correct libraries
Try compiling them separately:
$ gcc -Wall -c ioi.c
$ gcc -Wall -c driver.c
$ gcc ioi.o driver.o -o program -lncurses
To isolate and fix compilation errors...
You didn't mention if you are compiling on Windows or Unix. If on Windows I suspect that there are order dependencies in the .h files. Usually you want windows.h first so that it defines constants that the other .h files will use.

Resources