Unable to find header file - linux

I intend to include net/ip6_checksum.h in my code, but the compilation fails because the file is not found.
Searching for the file on the system shows:
$ find /usr -name ip6_checksum.h
/usr/src/linux-headers-3.2.0-52/include/net/ip6_checksum.h
/usr/src/linux-headers-3.2.0-54/include/net/ip6_checksum.h
The makefile looks for headers under /usr/lib
How do I include the file in my code?
Thanks!

You just need to add -I/usr/src/linux-headers-3.2.0-54/include to your compilation command.

Related

Linux Kbuild: what is the difference between $(src) and $(obj)

In Documentation/kbuild/makefiles.txt chapter 3.10 it is mentioned that $(src) refers to the location of the source code while $(obj) refers to the location of the generated output files. I am confused about this when using a different output directory.
In Makefile.build the very first thing that is done is src := $(obj). How does that make any sense? If I print $(src) and $(obj) they always have the same value.
However, what is even more confusing to me, is that if this was the case, make should issue an error.
If the working directory is outside the kernel source (O=path/to/out/dir) when the rule $(obj)/%.o: $(src)/%.c is evaluated it should search for the source file relative to the output directory. And since the source file is not there it should fail saying it cannot find a rule for $(src)/%.c target.
Can someone please explain what I'm getting wrong here?
Answering my own question in case others wondered about this...
The main Makefile uses vpath to add the src location, so when kbuild does not find the source file in the output tree it will find it in the source tree.

don't understand the difference between "gcc -c file.c" and "gcc -o file.c" command

Command :
`gcc -c -Wall hello.c`
Here is the error : while calling ./hello.o
bash: ./hello.o: cannot execute binary file: Exec format error
need help please ..
.o is an object file, not an executable. It's an intermediate step. The -c option just says to make that step. You'll still have to link that object file into an executable.
These are the options you are asking for
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’.
Unrecognized input files, not requiring compilation or assembly, are ignored.
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.
Using the first option you will have an object file, not an executable so you cannot execute it

CMake recursively add all source files inside all subdirectories of a directory to the executable?

I have a pretty big file structure of a project which I need to convert into a multiplatform cmake project. Now it seams that cmake requires ever single cpp file be added individually to the executable. But is there a script that automates this? That snoopes through the file structure and just adds every source file automatically? Since the project will probably get a lot more source files and I probably wont be able to manually add every single one.
You could use execute_process() with a cmake -P script that uses globbing to recursively scan for source files which writes to an included file in your CMakeLists.txt i.e. something like:
"CMakeLists.txt":
execute_process(COMMAND ${CMAKE_COMMAND}
-D "RDIR=${CMAKE_CURRENT_SOURCE_DIR}"
-P "scansources.cmake"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
include("sources.cmake")
add_executable(myexe ${sources})
"scansources.cmake" (generates "sources.cmake"):
file(GLOB_RECURSE sourcelist
*.c
*.cc
*.cpp
*.cxx)
string(REGEX REPLACE "${RDIR}/" "" relative_sources "${sourcelist}")
string(REPLACE ";" "\n" sources_string "${relative_sources}")
set(sources_string "set(sources\n${sources_string})")
file(WRITE sources.cmake "${sources_string}")
The reason why this works is because execute_process() occurs at configure time.
You could, of course, generate sources.cmake via some other tool or IDE then you wouldn't need scansources.cmake or execute_process().

Linux: no such file or directory

I am new in Linux, can anyone tell me which directory the computer search for "ansinist.h"? Below is the syntax:
USER#USER-PC /cygdrive/f/Dataset_extract/500ppi-Legacy/SRC/BIN/TXT2NIST
$ make -f makefile.mak
gcc -ansi -O2 -I/include -L/lib -c txt2nist.c
txt2nist.c:15:22: fatal error: ansinist.h: No such file or directory
#include <ansinist.h>
^
compilation terminated.
makefile.mak:53: recipe for target 'txt2nist.o' failed
make: *** [txt2nist.o] Error 1
This answer could help you. In general case, be sure that you have installed the libraries that you're going to use in your project.
To find the file ansinist.h
sudo find / -name ansinist.h
And please paste your output here but you should be sure you installed all the required libraries first..
1)if your header file is in current directory then use #include "ansinist.h"
because this syntax search directly into current directory.
2)if your header file is in /usr/include/ then #include< ansinist.h>
because this syntax first search into /usr/include/ then current directory.
3)also you can use #include < /path/ansinist.h>
where path=path where is header file.
4)if above things will not work then please give value of #echo $PATH for next i can help you.

How to group header files in header subfolders under the general header files folder using Cmake

Hi I am building Cmake based build system.
I would like to group header files in folders (VC++ can do it) under the general folder Header Files.
Similar, I would like to group the corresponding .cpp files in folders under the Source Files directory.
Unfortunately I could not find any instructions of how to do it.
Please help.
Dimitre
You should give a look at CMake source_group command.
You can use source_group. Here's a concrete example.
Suppose you have a directory structure like:
|-include
| some.h
|-sub
| someother.h
|-src
| some.cpp
|-sub
|-someother.cpp
Collect the files (some people - including the documentation - discourages use of GLOB, but I leave that to you, you can list of them one by if you want to, though I find GLOB is just fine many times):
file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB HEADER_FILES_SUB "${CMAKE_CURRENT_SOURCE_DIR}/include/sub/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h")
file(GLOB SOURCE_FILES_SUB "${CMAKE_CURRENT_SOURCE_DIR}/src/sub/*.h")
# Setup your library or executable:
add_library(MY_LIB ${HEADER_FILES} ${HEADER_FILES_SUB}
${SOURCE_FILES} ${SOURCE_FILES_SUB})
# Here's the important part ("Header Files" and "Source Files" are literals.)
source_group("Header Files\\sub" ${HEADER_FILES_SUB})
source_group("Source Files\\sub" ${SOURCE_FILES_SUB})

Resources