getting undefined references from libssh2 - c

I installed the libssh2 library on my Ubuntu linux. When compiling the C code I received this message:
undefined reference to `libssh2_session_init_ex'
I downloaded the libssh2 from their website. I followed the instructions of how to install it on my computer but It seems i am still missing soemthing.
This is my code in my make file:
all: ssh
ssh: mysshpass.c
gcc -lssh2 -g -o mysshpass mysshpass.c
clean:
rm -rf mysshpass
The include part of the C code:
1 #include <stdlib.h>
2 #include <libssh2.h>
3 #include <libssh2_sftp.h>
4
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 //#include <libssh2_config.h>
13
14 int main(int argc, char *argv[])
Thanks very much in advance
Note :
I used this new line :
all: ssh
ssh: mysshpass.c
gcc -g -Lusr/src/libssh2-1.4.1/include -o mysshpass mysshpass.c
clean:
rm -rf mysshpass
but still same compilations errors. By the way, I was told to install the libssh2-devel also, but i have no idea how to do that.
Thanks

1) Uninstall what you've got. There's something wrong, and it's being there can only confuse things. Get rid of it.
2) *install" libssh2-dev like this:
sudo apt-get install libssh2-dev libssh2
3) Try building your application again.
Make sure your makefile or build scripts point to the right place ("-I" for compile-time headers, "-L" for link-time libraries)
Good luck!

I think you want:
gcc -g \
-I /usr/src/libssh2-1.4.1/include \
-L /usr/src/libssh2-1.4.1/ \
-l ssh2 \
-o mysshpass mysshpass.c

You may put -lssh at the end.
Use:
gcc a.c -lssh

Related

How compile c code with python header file

I downloaded python source code using the command
git clone https://github.com/python/cpython
Now I created a main.c like so
#include <stdio.h>
#include <python.h>
int main(void)
{
return 0;
}
when I try to compile using this command
gcc main.c -L /f/<redacted>/cpython/Lib -I /f/<redacted>/cpython/Include -lpython
I get this error
$ gcc main.c -L /f/<redacted>/cpython/Lib -I /f/<redacted>/cpython/Include -lpython
In file included from main.c:2:
F:/<redacted>/cpython/Include/python.h:12:10: fatal error: pyconfig.h: No such file or directory
12 | #include "pyconfig.h"
| ^~~~~~~~~~~~
compilation terminated.
I know that pyconfig.h can be obtained using sudo apt install python-dev on Linux, so I tried pacman -S python-devel but this does not seem to fix the issue
I also tried to use locate pyconfig.h to link it using the -I but it is nowhere to be found
My machine is a windows 10 and I am using MSYS2withMINGW64 to compile this code

Do I need to compile separately included header files from another direcotry?

I'm working on a project (hangman.c) and I want to include a module I wrote from another directory (quicksort.h). Here is the top of my hangman.c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../quicksort/quicksort.h"
I've been running this command:
clang -o hangman hangman.c
And i get this error message:
/usr/bin/ld: /tmp/hangman-3292ab.o: in function `append_and_order':
hangman.c:(.text+0x26c): undefined reference to `quicksort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile them separately and link them together manually with
gcc -c hangman.c
gcc -c ../quicksort/quicksort.c
gcc -o hangman hangman.o quicksort.o
(As corrected). And that works, but isn't the point of #include to do this automatically at the preprocessing stage?

Link libsndfile source code with gcc

I just cloned libsndfile and created program.c file with the following contents:
#include <stdio.h>
#include <sndfile.h>
main() {
printf("hello world");
}
My goal is to get this file to compile using gcc (if indeed sndfile.h is the right header to include), however, I am not competent with c code nor the gcc compiler.
So far I've been referencing the gcc docs and the libsndfile FAQ and haven't been able to come up with a solution. I'm not sure if I even have a 'library' yet to feed the gcc -l option. Do I need to go through some sort of build process with the libsndfile source code first or can I 'link' to .c files?
So far, with program.c and the libsndfile clone directory in the same directory, I've tried the following:
gcc 'pkg-config --cflags libsndfile' program.c -o hello
gcc 'pkg-config --cflags ./libsndfile/sndfile.pc.in' program.c -o hello
I'm coding on my raspberry pi model B and did not install libsndfile, so, nothing is on my path... maybe I should rename sndfile.pc.in to sndfile.pc and somehow put it on my path? (not a linux guru either!)
If there are some resources you think I should study please comment! I'm perfectly willing to accept answers that simply push me enough to figure things out. Any help would be much appreciated.
First make sure you have gcc installed:
sudo apt-get install gcc
Then, install libsndfile1-dev:
sudo apt-get install libsndfile1-dev
I slightly fixed you code to avoid some warnings:
#include <stdio.h>
#include <sndfile.h>
int main(void) {
printf("hello world\n");
return 0;
}
Now you can compile:
gcc -o hello program.c
Execution:
bebs#rasp:~$ ./hello
hello world
bebs#rasp:~$
A small supplement of #alpereira7's answer.
It would cause linking failure without its lib. gcc -lsndfile -o hello program.c should be a 100% solution.

term.h : header not found

I have a little piece of code for linux terminal capabilities, it uses the term.h header file
#include <stdio.h>
#include <term.h>
#include <curses.h>
#include <stdlib.h>
main()
{
setupterm("unlisted",fileno(stdout),(int*)0);
printf("Done.\n");
}
but when I try to compile it I get fatal error: term.h : No such file or directory
What should I do? Where's the problem
On Ubuntu, download and install the libncurses5-dev:
sudo apt-get install libncurses5-dev
then compile:
gcc my_program.c -o my_program -lncurses

GNU-make 4. Running an example for "Loading Dynamic Objects"

The latest version of GNU-Make http://www.gnu.org/software/make/
provides many advanced capabilities, including many useful functions.
(...) On systems which support dynamically loadable objects, you can
write your own extension in any language (which can be compiled into
such an object) and load it to provide extended capabilities... http://www.gnu.org/software/make/manual/make.html#Loading-Objects
I tried to run the simple example below (a $(hello string) function). It works if I first compile the hello.so. But it doesn't work if I run it as the example provided here (with a load directive) http://www.gnu.org/software/make/manual/make.html#Loading-Objects . Make4 is installed in the current directory.
./Makefile:
all:
echo $(hello world)
load hello.so
hello.so: hello.c
$(CC) -shared -I./include -fPIC -o $# $<
./hello.c:
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <gnumake.h>
int plugin_is_GPL_compatible;
char * hello(const char *nm, unsigned int argc, char **argv)
{
int len = strlen (argv[0]) + 7;
char *buf = gmk_alloc (len);
sprintf(buf,"Hello %s",argv[0]);
return buf;
}
int hello_gmk_setup ()
{
gmk_add_function("hello", hello, 1, 1, 1);
return 1;
}
running the example:
./bin/make -v
GNU Make 4.0
Built for i686-pc-linux-gnu
$ ./bin/make
Makefile:4: hello.so: cannot open shared object file: No such file or directory
Makefile:4: *** hello.so: failed to load. Stop.
How can I run this example with the 'load' directive ?
I suggest in a comment to use
-load hello.so
instead of just load hello.so; this is analog to using -include in a Makefile.
The logic is that make plugins are generally expected to exit before you run some make using them (often, you would use a recursive make, e.g. run $(MAKE) -C subdir in a toplevel Makefile and ensure that the plugin does exist when your run $(MAKE) -C subdir)
If hello.so does not exist when -load hello.so is parsed, the GNU make would ignore that directive. (I am not sure you want that for a real plugin).
I still think that make plugins should generally not be built by the Makefile which is load-ing them.
I also believe that using Guile extensions in make is wiser than using plugins.

Resources