undefined reference to 'mbstowcs_s' - c

This is my code. I want to test function mbstowcs_s.
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
#include <assert.h>
int main()
{
char MultiByteStr[20] = "a中";
wchar_t WideByteStr[20];
printf("%s\n", MultiByteStr);
char* LocaleInfo = setlocale(LC_CTYPE, "");
printf("%s\n", LocaleInfo);
// required length of WideByteStr
size_t WideStrLen = 0;
mbstowcs_s(&WideStrLen, NULL, 0, MultiByteStr, 0);
assert(WideStrLen < 20);
mbstowcs_s(NULL, WideByteStr, 20, MultiByteStr, WideStrLen);
WideByteStr[WideStrLen] = L'\0';
wprintf(L"%ls\n", WideByteStr);
return 0;
}
I test this code on windows platform successfully, but failed on linux platform. Below is the error message.
test.c: In function ‘main’:
test.c:20:5: warning: implicit declaration of function ‘mbstowcs_s’ [-Wimplicit-function-declaration]
mbstowcs_s(&WideStrLen, NULL, 0, MultiByteStr, 0);
^
/tmp/ccqzP6HF.o: In function `main':
test.c:(.text+0x77): undefined reference to `mbstowcs_s'
test.c:(.text+0xc3): undefined reference to `mbstowcs_s'
collect2: error: ld returned 1 exit status

Related

undeclared function RegGetValue MinGW C compiler

I am trying to compile a C program using MinGW on Windows 7 (64-bit). The code is given below:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
void readRegDwordValue() {
HKEY hKey = HKEY_LOCAL_MACHINE;
char const *subKey = "Software\\Metpl\\My Program";
char const *pValue = "MJP_XXX";
uint32_t flags = RRF_RT_REG_DWORD;
int *pvData = NULL;
int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));
if (result != ERROR_SUCCESS) {
printf("Error getting value. Code: ");
printf("%" PRId64 "\n", result);
} else {
printf("Value data: ");
printf("%" PRId32 "\n", *(int32_t*)pvData);
}
}
int main() {
readRegDwordValue();
return 0;
}
I get the following warning:
gcc -O3 -Wall -c -o readReg.o readReg.c
readReg.c: In function 'readRegDwordValue':
readReg.c:13:22: warning: implicit declaration of function 'RegGetValue'; did you mean 'RegSetValue'? [-Wimplicit-function-declaration]
13 | int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));
| ^~~~~~~~~~~
| RegSetValue
I have included windows.h which includes winreg.h that contains the definition of the RegGetvalue function. Why is the compiler not able to find it? Also, since it is suggesting that I meant RegSetValue, does it mean it is able to find this one? !!
The linker gives the following error:
gcc readReg.o -o readReg.exe -L -liphlpapi -ladvapi32
d:/__sdk/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: readReg.o:readReg.c:(.text+0x45):
undefined reference to `RegGetValue'
collect2.exe: error: ld returned 1 exit status
What am I missing here? I have been pulling my hair over this for over 8 hours now and not able to understand where I am making the mistake. I have not been able to find much relevant discussion online on this either.
Desperately request some input on this so that I can move forward. Thanks in advance.
Finally! All the hair-pulling bore fruit. And, all the peripheral learning along the journey now seems exhilarating. Following is what works with MinGW-w64 (32-bit):
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
char buffer[1024]; // Based on need. This is the max that I need.
DWORD bufferSize = sizeof(buffer);
DWORD readRegDwordValue(HKEY hKey, char const *subKey, char const *pValue) {
long unsigned int *p = NULL;
int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_DWORD, p, &buffer, &bufferSize);
if (result != ERROR_SUCCESS) {
return 4294967295; // max unsigned int.
}
return ((DWORD *)buffer)[0]; // return the first element of the buffer.
}
char * readStringValue(HKEY hKey, char const *subKey, char const *pValue) {
long unsigned int *p = NULL;
int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_SZ, p, &buffer, &bufferSize);
if (result != ERROR_SUCCESS) {
return "Error";
}
return (char *)buffer;
}
int main() {
char *string = readStringValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "companyName");
printf("string:%s%s%d%s \n", string, " (length = ", strlen(string), ")");
DWORD integerValue = readRegDwordValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "sampleInteger");
printf("integer:(%lu%s\n", integerValue, ")");
return 0;
}
I have tested this code and it gives correct results. Since I only need to get the correct result or an error, I have not added additional error-checking in the code.
Thanks, #CGio3, for pointing me to MinGW-w64. I have made many Portable C programs and a few Windows programs with the version I have and got too comfortable with it, I guess. Anyway, lesson learnt.
Hope this helps someone who has been struggling with the subject.

C invalid initializer on empty line and invalid arguement on a while loop

I am getting those following errors in this code I am not sure what causes them. Since one of them happens on an empty line where nothing happens. The other is on a while loop that doesn't take an argument I believe.
Error message:
test.c: In function ‘main’:
test.c:28:15: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
28 | checker = decrypt(Ciphertext,key,iv);
| ^
test.c:31:8: warning: format not a string literal and no format arguments [-Wformat-security]
31 | printf(key);
| ^~~~~~
/usr/bin/ld: /tmp/ccGWk1m2.o: in function `main':
test.c:(.text+0x57): undefined reference to `readWord'
/usr/bin/ld: test.c:(.text+0x8c): undefined reference to `decrypt'
collect2: error: ld returned 1 exit status
#include <stdio.h>
#include <stdlib.h>
#include <conf.h>
#include <evp.h>
#include <err.h>
#include <string.h>
#include <ctype.h>
unsigned char *readword(FILE *fp);
int decrypt(unsigned char *Ciphertext, unsigned char *key,unsigned char *iv);
int main (void)
{
char *key = NULL;
char *iv = "aabbccddeeff00998877665544332211";
char *Plaintext = "This is a top secret.";
char *Ciphertext = "764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2";
char *checker;
// invalid initializer on this line for some reason
FILE words;
words = (fopen("words", "r"));
while(1) { // invalid argument when I am not even using one? it's C standard loop right?
key = readword(words);
if (!key){
printf("the key was not found");
break;
}
else{
checker = decrypt(Ciphertext,key,iv);
if(strcmp(checker,Plaintext)){
printf("the correct key is ");
printf(key);
}
free(key);
free(checker);
}
}
return 0;
}

How to list info about processes using c in freebsd?

I want to list all processes in FreeBSD and I have this code below, which uses kvm, but it does not know what KVM_NO_FILES is, and I can't figure how to fix it. If there is another way of doing it, please do share.
#include <stdio.h>
#include <kvm.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int
main(void)
{
char errbuf[_POSIX2_LINE_MAX];
kvm_t *kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
int nentries = 0;
struct kinfo_proc *kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
int i;
for (i = 0; i < nentries; ++i) {
printf("%s\n", kinfo[i].p_comm);
}
return 0;
}
And I get this error:
root#freebsd:- # cc -lkvm main.c
main.c:11:53: error: use of undeclared identifier 'KVM_NO_FILES'
kvm_t *kernel = kvm_openfiles(NULL. NULL. NULL, HVM_NO_FILES, errbuf):
main.c:13:71: error: invalid application of 'sizeof' to an incomplete type
'struct kinfo_proc'
...= kvm_getprocs(kernel, HERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nen...
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc'
struct kinfo_proc;
main.c:18:29: error: subscript of pointer to incomplete type 'struct kinfo_proc'
printf("Xs\n", kinfolil.p_comm):
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc'
struct kinfo_proc;
3 errors generated.

warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

I'm using the following C code:
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
if(lseek(file,10,SEEK_SET) < 0) return 1;
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
return 0;
}
After compiling it produces a warning:
warning: incompatible implicit declaration of built-in
function ‘printf’ [enabled by default]
What does it mean and how do I appease the C compiler to not raise the warning?
You need to add #include <stdio.h> to the top of your file.

Thread error while compiling the Xml parser in C?

[root#localhost mxml-2.7]# gcc -o xml XmlParser1.c -lmxml
XmlParser1.c: In function ‘main’:
XmlParser1.c:63: warning: assignment discards qualifiers from pointer target type
/usr/local/lib/libmxml.so: undefined reference to `pthread_key_create'
/usr/local/lib/libmxml.so: undefined reference to `pthread_once'
/usr/local/lib/libmxml.so: undefined reference to `pthread_getspecific'
/usr/local/lib/libmxml.so: undefined reference to `pthread_key_delete'
/usr/local/lib/libmxml.so: undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status
Following error occurred while compiling the XmlParser1.c.
XmlParser1.c:
#include <stdio.h>
#include "mxml.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAX_SIZE 25
static mxml_node_t *xml;
static mxml_node_t *str;
static mxml_node_t *DataSet;
static mxml_node_t *Table;
static mxml_node_t *IsAuthenticated;
static mxml_node_t *AuthenticationDate;
static mxml_node_t *Response;
int main()
{
int fd = 0;
char *Result=NULL;
const char *NewResult=NULL;
mxml_node_t *tree;
mxml_node_t *data;
const char *type = NULL;
FILE *fp = fopen("/home/suneet/mxml-2.7/Sample/main.xml", "r") ;
if (fp == NULL)
{
fclose(fp);
}
else
{
fseek (fp , 0, SEEK_END);
long settings_size = ftell (fp);
rewind (fp);
if (settings_size > 0)
{
tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
fclose(fp);
printf("step 1\n");
data = mxmlFindElement(Table, tree, "diffgr:id", NULL, NULL, MXML_DESCEND);
Result = mxmlElementGetAttr(data,"diffgr:id");
printf("diffgr:id:%s\n",(Result == NULL)?"NULL":Result);
mxmlDelete(data);
mxmlDelete(tree);
}
}
return 0;
}
while i am trying to go with the steps given in http://minixml.org/; parse accordingly the xml file there is certain error "undefined error to threads for dynamic library libxml.so.".
Please guide me so that i can successfully parse the xml file.
You need to link with the pthread library using the -pthread option.
gcc -o xml XmlParser1.c -lmxml -pthread
You need to include pthread in you code.
add this line at the beginning of your file
#include<pthread.h>

Resources