how to use crypt( ) method in Linux? - c

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!

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

CLion not finding header in main.c for use in external .c file

So my situation is something like this, I have three files:
main.c:
#include <stdio.h>
#include "hello.h"
int main() {
hello();
}
hello.h:
void hello();
hello.c:
void hello() {
printf("Hello");
}
My Cmake file looks something like this:
cmake_minimum_required(VERSION 3.3)
project(test)
set(SOURCE_FILES main.c hello.c)
add_executable(test ${SOURCE_FILES})
The code runs fine. However CLion doesn't recognise the printf() function in hello.c, and wants me to add it as a header file. Is there a way to make it see the #include <stdio.h> in the main.c file, and stop giving me a hard time?
So I fixed this to my own satisfaction by making the functions defined in my .c files return values rather than calling printf inside those functions. Then printing the values returned in main.c
hello.c and main.c are independent compilation units and as such needs to have #include <stdio.h> in both. Actually in your example having #include <stdio.h> in main.c accomplishes nothing as nothing forward declared there is being used in hello.h nor main.c.
You should actually be seeing warnings when compiling hello.c on its own.
When the compiler finds a function it does not know (has not been declared yet), it assumes it has the signature int function_X(void). So for your case it will be wrong for printf which has int printf(char const*, ...). But you are lucky, due to the way that arguments are passed in your platform, everything works out.
So you basically need to forward declare functions to ensure that when compiling the compiler knows where to place the arguments so that the called function can find it.
There is more to it but this short explanation should be enough for a beginner and if you read one of the books in the link I provided in the comments you should be able to understand it better.

How to use PlaySound in C

I am using code::blocks IDE which runs on GNU GCC compiler. In my project I want to play a .wav sound file in C. I tried to play a .wav sound file with a function called PlaySound. When I compiled the code code::blocks gave me an error - PlaySoundA not declared. My code is-
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
int main(int argc, char *argv[])
{
PlaySound("C:\Snakes and Ladders\snake.wav",NULL,SND_SYNC | SND_LOOP | SND_FILENAME);
return 0;
}
I checked my path twice. I read about this function on the internet and as per me I am using it in the correct way.
In Google, I read that the function exists in a file called winmm.lib. So I put a line of code after all the headers. It was-
#pragma comment (lib , "winmm.lib")
I also added the name winmm.lib to the additional dependencies of code::blocks. So now when I compile the code it gives me another error - winmm.lib not found. Can somebody please tell me how to use PlaySound correctly.
Remove the pragma comment
Double the backslashes. The backslash is an escape character
Compile with the winmm library. Using MinGW, the command would look like this:
gcc foo.c -o foo.exe -lwinmm
Go to Settings - compiler... - linker settings. on the right side in other linker option write this:-lwinmm

How can I compile code that uses getsubopt()?

I want to parse a list of options of the form key1=val1, key2=val2, etc (like the options to mount -o). The getsubopt() function seems perfect for this task (http://www.gnu.org/s/hello/manual/libc/Suboptions.html). However, when I try to compile my code using gcc, I get:
warning: implicit declaration of function ‘getsubopt’
and the program segfaults when I run it.
I added #include <stdlib.h> but the compiler doesn't pick up the declaration.
Do you have:
#define _XOPEN_SOURCE 500
#include <stdlib.h>
at the top of the file that contains the call to getsubopt? The error you are getting is what you would expect if you call a function which has not been declared.

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