GCC Undefined reference. Cannot locate Library - c

So i'm trying to create .so files for use in the ctypes library for python but am running into a bit of a snag.
The files in the directory are cbw.h, cbw64.lib, and Main.c
During my attempt of creating the .so file using the following command string in Windows Powershell:
gcc -shared -o testlib.so -fPIC Main.C -lcbw
I get the error 'cannot find -lcbw'.
I put this in, because if I don't, I get that there are 'undefined references' in Main.C which were defined during the 'include cbw.h' line in Main.c
What is wrong with my command? I am able to create a .so file if I use a simple program like Hello world but cannot do so as soon as I try to import predefined functions.
edit:
If i use the object library cbw64.lib I get the error, ' error adding symbols: file in wrong format'

Related

Compiling C file which includes other header files

I am trying to compile a file Mv.c like - g++ microtime.c Mv.c
It gives an error - v.c:2:10: fatal error: microtime.h: No such file or directory 2 | #include <microtime.h>
My current directory has both microtime.h and microtime.c and Mv.c includes microtime.h
I am not sure how to go about compiling it.
Since my main program Mv.c is using microtime.h do I need to compile microtime.c first and pass it as an argument to g++?
I got it compiled by using the command g++ -I. Mv.c microtime.o
where microtime.o I generated using g++ -c microtime.c
I am not sure why this command works and why we need to specify the extra -I. option when I have already created the compiled object file microtime.o
If you write #include <microtime.h>, the compiler uses its list of system directories to look for the header file. By the option -I you add a directory to this list, and the compiler is happy.
To include project-local header files, use #include "microtime.h".
Read the chapter "Directory Options" in GCC's documentation to learn more.

How to use the hidapi library from Signal11?

I installed the hidapi library from Signall11 on my windows10 pc (using minGW). But now I'm having some trouble actually getting it to work with gcc. I have some main.c file in which I include the hidapi.h file. My gcc command looks like
gcc main.c
I'm not sure where I'm going wrong because whenever I try to run this command I get an undefined reference error to some function that is defined in the hidapi.h file.
A full compile command for a project using hidapi is like this:
gcc -o your_app your_app.c -lhidapi-hidraw
It's not enough to include #include "hidapi.h" in the C-code, which does let gcc compile. You also need -lhidapi-hidraw to link with the library. I.e. compiling is in fact a 2 step process.

Compiling c program with dependencies, h and h0 files

I am trying to compile the gjh solver - written in C - into an executable file in windows. It is available on netlib
I downloaded the c file and am using gcc compiler via WinGW on windows' command prompt. Trying to compile the gjh.c file directly gave me an error that says:
gjh.c:33:21: fatal error: getstub.h: No such file or directory
#include "getstub.h"
compilation terminated.
I assumed that compiling gjh.c requires the dependency getstub.h.
getstub.h is not the only dependency required, there are other dependencies, namely: arith.h, asl.h, funcadd.h, and stdio1.h. All of these files are available on the same link where I found getstub.h. However, arith.h0 and stdio1.h0 are available instead of arith.h and stdio1.h.
Are these files the same? I tried to rename the .h0 files to .h and tried to compile gjh.c, but I got this error:
collect2.exe: error: ld returned 1 exit status
Are the two files the same? If not, is there any way for me to compile the gjh solver successfully into an .exe?
If that's the only problem in compiling, try using the -I switch in gcc:
gcc -I/my/path/to/include/files -o gjh gjh.c
the -I switch hints to gcc where to find your #include files.
I am not sure about the stdio1.h. I think your approach to rename is OK but that reference to external functions such as Sprintf. You need to link with a library defining that. If you know where it comes from, use the -L and -l switch in gcc for that:
gcc -I/my/path/to/include/files -L/my/path/to/library -lnameoflibrary \
-o gjh gjh.c

APUE.H Error Linux C programming

I'm trying to execute the below program-
Within APUE.3E -> filedir -> filetype.c (this comes by default when I downloaded APUE.3E. I didn't make any changes)
but when I compile this is the error I'm receiving:
myramya~/Documents/apue.3e/filedir$ gcc filetype.c -lm -o filetype
/tmp/cchPKE7K.o: In function main':
filetype.c:(.text+0x94): undefined reference to err_ret'
collect2: error: ld returned 1 exit status
I'm using Linux Ubuntu. I have installed APUE.3E in Documents folder. I have administrator permissions. I wrote a simple Hello.c program and executed using:
$ gcc hello.c -o hello
and it worked without any issues.
Your hello example works compiling in a single step with gcc because it does not call any functions in other files (except functions in the standard C library which allways gets linked in).
Your filetype.c makes calls to a function err_ret which is not within filetype.c but in some other source file.
When compiling bigger programs the work is usually done in two steps: First source files are compiled into object files by making one call to gcc with the flag -c for each source file. Then all object files are linked together with a single call to gcc with all object files. It is also possible to put object files together into libraries. Usually a Makefile is used to compile bigger projects.
Your specific case with apue.3e is well explained here: https://unix.stackexchange.com/questions/105483/compiling-code-from-apue

GCC shared library linked but header file in library not found

So I have some trouble using shared libraries in combination with GCC.
I'm quite new to this so that's why I came to ask it here. I was unable to find a question on stack overflow that helps me with mine (maybe i overlooked one). But first something about my setup and what I'm trying to accomplish.
So I have these two libraries that are development parallel to each other. One is named liblinkedlist, which contains the linkedlist implementation in C and the other is named libgraph, containing a graph implementation in C. Both are put in the following folder structure:
<root>
+---graph_lib(folder)
+---build(folder)
+---src(folder)
+---makefile(file)
+---linkedlist_lib
+---build(folder)
+---src(folder)
+---makefile(file)
Each src folder contains the source files (linkedlist.h and .c for liblinkedlist and graph.h and .c for libgraph)
In each build folder the .o and .so files created from the header files are stored.
Now the problem is that I want to use the liblinkedlist.so in /linkedlist_lib/build/ in my graph library. While compiling the libraries everything seems to go well. But when I try to use it in combination with including a header file (which is inside the linkedlist library), I get the message that it could not be found.
My compile commands are the following:
For the liblinkedlist:
gcc -fpic -c src/linkedlist.c -o build/linkedlist.o
gcc -shared -o build/liblinkedlist.so build/linkedlist.o
And for the libgraph wich uses the liblinkedlist:
gcc -fpic -c src/libgraph.c -o build/libgraph.o
gcc -fpic -c src/graph.c -o build/graph.o
gcc -L../linkedlist_lib/build/ -o build/libgraph build/libgraph.o build/graph.o -llinkedlist
These are the command and errors I get when using the header file:
gcc -fpic -c src/libgraph.c -o build/libgraph.o
In file included from src/libgraph.c:2:0:
src/graph.h:4:24: fatal error: linkedlist.h: File or folder does not exist
#include "linkedlist.h"
^
compilation terminated.
make: *** [build/libgraph.o] Fout 1
Any ideas on how to fix this problem, am I doing something wrong here?
Thanks in advance

Resources