GNU C compiler cannot locate header file - c

I have installed a package libstree on my machine (x86_64/Redhat Linux).
Have followed the instructions ( ./configure --prefix=/usr; make check; make install) to install the package.
Have checked that the relevant header files are in the /usr/include and /usr/lib directories.
However when I try to compile a test program I get an error message:
test.c:6:25: fatal error: lst_structs.h: No such file or directory
compilation terminated.
lst_structs.h is present in the /usr/include/stree directory.
Anyone have any thoughts as to why the GNU C compiler cannot locate the header file?

As it's in an /include sub-directory, you need to explicitly mention it (see comments above), or adjust your lib include path, as outlined on this old stackoverflow post:
How to add a default include path for gcc in linux?

Related

Using protoc inside CMakeLists from a non-standard directory gives "cannot open shared object file: No such file or directory" error

I have a C application using CMake to generate Makefiles on Linux. The application contains .c as well as .proto files. Now I need to genearte .pb-c.c and .pb-c.h using protoc command in the CMakeLists.txt so that when I do cmake . the cmake generates the corresponding .pb-c and .pb-h. The protoc is used as:
execute_process(COMMAND bash -c "${PROTOC_PATH} --c_out=${CMAKE_CURRENT_SOURCE_DIR}/ --proto_path=${PROTO_DIR}/ ${PROTO_DIR}/*.proto")
The problem is that my protoc binary and related .so file is not in /usr/bin and /usr/lib or /usr/local/bin and /usr/local/lib. They are in a directory inside the project - $HOME/project-name/dependencies/bin/protoc and $HOME/project-name/dependencies/lib/libprotobuf.so.12
Due to this I am getting error - error while loading shared libraries: libprotobuf.so.12: cannot open shared object file: No such file or directory
But if I give the command as
execute_process(COMMAND bash -c "protoc--c_out=${CMAKE_CURRENT_SOURCE_DIR}/ --proto_path=${PROTO_DIR}/ ${PROTO_DIR}/*.proto") and run cmake . then it works fine as linker is able to get the .so file from /usr/lib
Mentioned below is a part of my CMakeLists.txt file
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath=${PROTOC_LIB_PATH} -L${PROTOC_LIB_PATH} -lprotobuf")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-rpath=${PROTOC_LIB_PATH} -L${PROTOC_LIB_PATH} -lprotobuf")
execute_process(COMMAND bash -c "${PROTOC_PATH} --c_out=${CMAKE_CURRENT_SOURCE_DIR}/ --proto_path=${PROTO_DIR}/ ${PROTO_DIR}/*.proto")
But it's not working due to aforementioned error.
Also for those who might say it's a duplicate I have looked into and tried the following SO questions:
Turning on linker flags with CMake
I don't understand -Wl,-rpath -Wl,
CMake link to external library
Does cmake have something like target_link_options?
CMAKE RPATH not working - could not find shared object file
https://serverfault.com/q/279068/435497
How to add linker flag for libraries with CMake?
https://serverfault.com/a/926072/435497
If you do not have a special use case, you do not need to call protoc yourself. Let CMake do this for you.
Have a look at: https://cmake.org/cmake/help/v3.16/module/FindProtobuf.html
and Cmake : find protobuf package in custom directory
I found this link which has a script showing how to create a PROTOBUF_GENERATE_C function which can then be used to generate the .pb-c abd .pb-h files.
From the above script I got the idea to make use of find_program which is similar to find_library in a way that it lets you pass the PATHS/PATH option so that CMake looks for the required program in the mentioned path.

Using gcc for compiling/converting, matrix.h: No such file or directory

I'm trying to convert some .c files to R files using gcc.
The files im talking about are circulant.c and circulant.h files located at:
http://www.columbia.edu/~ad3217/fbm.html
There the author also provided short instructions how to do it with gcc.
Things I did so far:
installed Ubuntu via the Win10 Linux Subsystem
installed build essentials
installed gcc
downloaded all meschach files from
http://www.netlib.org/c/meschach/
putting the meschach folder and circulant.x files into one folder and running the command
$gcc -I meschach/ -c circulant.c
gives the error:
In file included from circulant.c:25:0:
circulant.h:11:20: fatal error: matrix.h: No such file or directory
compilation terminated.
I hope that someone with more knowledge with gcc can help me out and point me to a solution.
You aren't using the right header files.
You have some dependency issues you need to take care of first. From the comment header block of circulant.c:
/* This program simulates fractional Gaussian noise or fractional */
/* Brownian motion using the Wood and Chan algorithm. */
/* The C-packages Ranlib and Meschach are used, both available */
/* via Netlib (http://www.netlib.org). */
Looks to me like the missing matrix.h and matrix2.h files are from the meschach library.
You can't just copy the .shar files into a directory.
You need to read the README file and follow the installation instructions. So I would recommend installing the ranlib and meschach dependencies first.

Header files for fftw missing?

I installed a new system and all of the libraries on to it. However, I have problems with fftw. After cmake command I get the following error
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Could NOT find FFTW (missing: FFTW_LIBRARIES FFTW_INCLUDES)
I can find where the FFTW libraries are (/usr/lib64/), but I can't find where header files for fftw are located. How can I find them? Also, for some other header files: is it safe to put a symbolic link in /usr/includes/ instead of giving the full path in .c code?
You can use the Unix command locate to determine the location of the FFTW header files
$ locate fftw3.h
/usr/local/include/fftw3.h
If you install FFTW 3 from source the header file fftw3.h should be placed in /usr/local/include/ by default.
The FFTW header files for FFTW 2 are contained in the package fftw-dev and for FFTW 3 are contained in the package libfftw3-dev.
The /usr/local directory is intended to contain all the user binaries, their documentation, libraries, header files, etc.... so it is ok to symlink library files in there, providing you do it correctly.

Statically Linking glib

I'm trying to statically link glib into my C program. I'm not sure what's the best way to do this. I downloaded the code and put it in a subdirectory called glib-2.36.4. I added "-Iglib-2.36.4" when using gcc. The glib.h is in the glib-2.36.4/glib directory and in that file there are references to other header files under the glib directory (such as #include ).
I'm not sure why that is since both glib.h and these other header files are at the same level (in glib subdirectory). I got a compile error due to galloca.h not being found (even though it's there). So I copied glib.h up one level and those errors went away. I then got an error about a missing glibconfig.h. I copied that from my usr directory and that error went away. I compiled my project and now I'm getting an error about undefined reference to g_ptr_array_new. I guess this must be because I haven't actually compiled glib. I had tried to build glib, but when I typed "./configure", but I got this message:
checking if arpa/nameser_compat.h is needed... configure: error: could not compile test program either way
I did install glib using yum, but I really want this code to run even if glib is not installed on a machine.
You need to install both glib and glib-dev via yum, compile using ./configure, (take a look in the ./configure script to see if there are any flags you need to supply or defines you need to produce the static build), without moving any files about, and then you need to compile your code using -i path/to/glib/includes and link with -L path/to/built/static/library

C Socket Programming with CygWin

Hi
I'm trying to write a little server / client program in C using CygWin, problem is, the gcc compiler in Cygwin doesn't seem to contain the standard headers assosiated with socket-programming. When trying to compile my server program, i get:
netinet/in.h: No such file or directory
sys/socket.h: No such file or directory
netdb.h: No such file or directory
Are these three headers located elsewhere in the CygWin-enviorment?
I also had same problem.. i was googling for hours and finally found this... thanks to ralph...
https://ralphexe.wordpress.com/2015/09/09/run-unix-socket-program-in-windows-using-cygwin/
For socket commands you have to include some of the packages in cygwin while installing...
(If you have installed try reinstalling ...)
During the installation :
In "SELECT PACKAGES " phase of installation.
Expand DEVEL
select GNU compilers for c and c++
4.And click next and complete the installation.
NOW, try with a socket program involving sys/socket.h ...
I HOPE IT WILL WORK ... :-)..
Try using the -I compiler command line option to specify a path to the header files.
My local reference states
-I dir
Add the directory dir to the list of directories to be searched for
header files. Directories named by -I are searched before the
standard system include directories. If the directory dir is a
standard system include directory, the option is ignored to ensure
that the default search order for system directories and the
special treatment of system headers are not defeated .
At least on my cygwim they are in
/usr/include/sys and /usr/include/netinet
I also had the same problem.
I've resolved to review the compile options.
NG:
$ gcc -mno-cygwin -o echo_server.exe echo_server.c
echo_server.c:12:43: sys/socket.h: No such file or directory
echo_server.c:13:24: netinet/in.h: No such file or directory
OK:
$ gcc -o echo_server.exe echo_server.c
I had this problem for days with the library apophenia .
so make sure the libraries you've installed have first been configured with a simple command
./configure
once configured use the following commands to build , Install and check the libraries
all- Builds libraries
install- installs libraries
check-tests the libraries
doc- generate documentation via doxygen
Hope it helps. :)

Resources