Need help in working with cs50 library, in window device - c

I'm using visual studio code, in window 10 device, and live in a area where internet accessibility is not good.
I have completed of lecturers of first week of cs50x.
Had extracted cs50.h and cs50.c and copy them to C:\msys64\mingw64\include\
Now after running the code, I'm getting undefined reference to 'get_int' and Id returned 1 exit status.
After going through some online solutions, I get this
In your source code, change #include <cs50.h> to #include <cs50.c>
But I'm not finding any <cs50.h> in cs50.c source file instead it has "cs50.h"
I'm not able to understand
How to link cs50 when compiling your code with clang, by using -lcs50

Related

Can't compile C in Devc++

I have tried running a simple piece of C code in DevC++.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("Temporary secretary");
return 0;
}
Every time i try to compile it, it makes "makefile.win" and points out an error on line 25.
screenshot
The error message in the build window is telling you exactly what the problem is - you have multiple definitions of main, one in main.c and the other in Untitled2.c.
Based on what you have posted, it's not clear why Untitled2.c is part of the build - I would remove it from the project completely.
In the future, please do not post links to images of screenshots - copy and paste any code (which includes makefiles) and error messages into the body of your question. We can't copy and paste from a screenshot, and chasing random links is unsafe.

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?

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

Linking md5.h library for HTTP Digest sample implementation

I am struggling to compile a simple C program from RFC 2617. The program is digtest.c and it uses digcalc.c, another file from the sample implementation. The latter one depends on two files that my compiler doesn't know about:
#include <global.h>
#include <md5.h>
At first I got this error:
digcalc.c:5:20: fatal error: global.h: No such file or directory
I resolved that by changing <global.h> to <stddef.h>, it seems. But I still get this error:
digcalc.c:7:17: fatal error: md5.h: No such file or directory
Now, md5.h seems to refer to the file found in libbsd. So I installed libbsd-dev and tried to compile the files like this:
gcc digcalc.c digtest.c -o digtest -L/usr/lib/x86_64-linux-gnu -lbsd
where /usr/lib/x86_64-linux-gnu is the location of libbsd.so and libbsd.a files. However, this does not resolve the last compilation error.
Could anyone point out what am I missing here?
Figured it out. Had to change <md5.h> to <bsd/md5.h>, as noted on libbsd page.
So instead of the original headers in digcalc.c:
#include <global.h>
#include <md5.h>
I used:
#include <stddef.h>
#include <bsd/md5.h>
Also had to change function stricmp to strcasecmp, its POSIX equivalent. After that the sample code compiled seamlessly.

Compile c code using gcc without installing mingW/cygwin

I have visual studio installed in my system. So its corresponding compiler and environment variables are set. When i try to compile c file using cl command, it works fine. Now i zipped mingW from another system and extracted it to my system. Say i have it in D:/mingW. Now i have created a batch file for compiling the c file. The contents of the batch file are,
set gccPath=D:/mingW/bin
%gccPath%/gcc.exe -c -std=c99 -o myC.o ../myC.c -I..\.
When i run this batch file, it is producing few errors.
One such error is
stdio.h:209:71: error: macro "snprintf" requires 5 arguments, but only 4 given
The above error might be due to the fact that compiler takes the stdio.h of visual studio instead of mingW's default header file.
Another error is,
error: previous declaration of 'xxxxFun' was here
What should i change in the batch script to compile the c file completely using mingW.
Compilation process is successful when we use Visual Studio, but throws error if we use gcc for the same set of files
EDIT:
I fixed the latter error.
Also the first error doesn't occur when stdio.h is included at first. But if we include stdio.h at the middle of the include section, the error comes.
#include <string.h>
#include <assert.h>
#include <minmax.h>
#include "myFunctions.h"
#include "MyModule.h"
#include <stdio.h>
When we have stdio.h at last as shown, the error is coming. If we move the line #include <stdio.h> to any other lines above #include <MyModule.h> , the specified error is not coming. Any reason behind this strange behavior?
Check whether you are defining snprintf using macros in any of your header files. This error may be caused due to incorrect/unnecessary macro.

Resources