Why no <string.h> in getopt library? - c

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".

Related

Xcode for C Implementation - Random Generator - Environment Problems

When you have a few files (see below), how do you get them all working inside Xcode for a C implementation?
These are the files that I’ve been given for this project:
pcg_basic.c
pcg_basic.h
pcg32-demo.c
Project guidelines:
http://www.pcg-random.org/using-pcg-c-basic.html
I am also unclear what should be copied to the main.c file. Can't get a grasp on this.
Apparently you also need to link the code with the pcg_basic.o.
(not sure how to do that, either)
I know the code does work because it was developed by an expert in this space. But everything I try in Xcode ("fails to build"), so I presume it must be the way I've set things up.
What am I doing wrong?
*Student here. I am totally new to programming. Thanks!
error snippet 1
error snippet 2
CODE
/*
* PCG Random Number Generation for C.
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
* http://www.pcg-random.org
*/
/*
* This file was mechanically generated from tests/check-pcg32.c
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include "pcg_basic.h" <---pcg_basic.h not found
To setup:
in Xcode <File/New/Project...>
choose macOS template tab
from there choose Command Line Tool
in the Wizard choose C as language
Drag & drop your files (pcg_basic.h, pcg_basic.c, pcg32-demo.c) to the Xcode project navigator on the left where main.c resides. A dialog appears: make sure to check Copy items if needed. Then delete main.c in Xcode.
The program builds and runs then.
Demo

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,

c - _setmode function causing debug error

Ok, so after posting this question I tried to use the solutions provided in the related questions (particularly this) pointed by the community but I had another problem.
When trying to use the _setmode() function to change the Windows console to print UTF characters I get a debug error, just like the one posted on this other question. The debug error is as follows:
Text:
Debug Assertion Failed!
Program:
...kout-Desktop_Qt_5_5_0_MSVC2013_64bit-Debug\debug\Breakout.exe
File: f:\dd\vctools\crt\crtw32\stdio\output.c
Line: 1033
Expression: ((_Stream->_flag & _IOSTRG) || ( fn = _fileno(_Stream), (
(_textmode_safe(fn) == _IOINFO_TM_ANSI) &&
!_tm_unicode_safe(fn))))
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
Screenshot:
Without the _setmode() function I still can't print characters from the upper ASCII Table, like these: "┌──┐". What can I do to solve this problem? The solution to the question with the same problem doesn't work also.
Again, I'm using Qt Creator on Windows, with Qt version 5.5.0 MSVC 64 bits. The compiler is the Microsoft Visual C++ Compiler 12.0 (amd64).
Edit:
Here's a small sample code that causes the error:
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
int main(void)
{
//Using setmode to force the error
_setmode(_fileno(stdout), _O_U16TEXT);
printf("Hello World!\n");
return 0;
}
Upon execution the error appears.
It seems that if you set the output mode to UTF-16, you must then use wprintf instead of printf.
(Presumably, since you have told the runtime to translate from UTF-16, you have to provide UTF-16.)
This code runs on my machine:
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
int main(void) {
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
return 0;
}
So does
wprintf(L"Hello world!\n");
PS - I'm not sure whether this will solve your underlying problem, which I suspect has to do with the encoding of the source file. Even if using UTF-16 does solve your problem, it probably isn't the best solution.

Eclipse project cannot include tchar.h

I am trying to build an lzmat_lib compression library using Eclipse with Cygwin gcc. I downloaded the library from the link http://www.matcode.com/lzmat_lib.zip. The file has the following include files:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <tchar.h>
#include "lzmat.h"
It cannot find the tchar.h header file. I do not understand how to add this header file. Please suggest a solution.
Your options are to install a Windows development environment, like Visual Studio or mingw along with the Windows SDK, or to port the code to your cygwin (posix) environment.
To port the code, you'd just do this:
Remove #include <tchar.h>.
Search and replace _TCHAR to char.
Search the file for all strings beginning with _t and remove that prefix. E.g., _tfopen becomes just fopen. _tprintf becomes printf.
Search for the text _T and remove it. You could also remove the extra parentheses that will then surround your string.
Deal with any other issues as they come up by removing the dependency on tchar.h and using a standard function instead.

Compile C program with visual studio

I am required to explain what the program located in the following links does:
main.c
csapp.c
csapp.h
I compile the following code in linux as:
............................................................
(note all three files have to be in the same working directory for compilation to work. )
that command is: gcc main.c csapp.c
when I execute that command I get the executable: a.out and I get no compilation errors!
That executable file can be downloaded from here (I don't think you need that file plus I will not execute that file if I where you).
Anyways I think that if I could debug the program I will be able to understand better what is going on. As a result I have created a C++ console empty console project in visual studio. I will like to include the same files in there and be able to compile it. I have never used c++ before and I don't really understand where to place header files. This is what I have done hoping to be able to compile the program:
The program will not compile if I place the files like that.
I have also tried placing all the files in the same directory just like on the linux virtual machine:
that does not compile either.
How will I be able to compile that program with visual studio?
If you look at csapp.h you'll notice it tries to include these headers:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
Some of these are std C headers, but others are specific to POSIX/Linux/UNIX style operating systems (pthread.h for example). You won't be able to use these libraries on Windows or in Visual Studio unless you're doing something unusual like compiling against Cygwin libraries.
If you want to get an understanding of what the program is doing, there's a number of things you could do. First off, just read through the code and look up the functions it calls in the man pages which document those functions (If you have gcc, I guess you also have man?) second, yes you could print to console to figure stuff out. You could also use a debugger like gdb to step through the program, it's not as intuitive as VS debugger but it works...

Resources