This forum has been archived. All content is frozen. Please use KDE Discuss instead.

KDevelop and #include "xxx.h"

Tags: None
(comma "," separated)
miriki
Registered Member
Posts
8
Karma
0
OS

KDevelop and #include "xxx.h"

Sat Jul 09, 2022 10:14 am
Hi, all!

I just stumbled across a (newbie?) question about including header files. (I'm just getting familiar with KDevelop (5.6.2) C with Debian, was using Visual-Studio VB.net and C# with Windows until now.)

I started a new session "Rheinwerk OpenBook", added a new project from template "Standard - Terminal - CMake C". The "hello world" was displayed in the out window after "Build" and "Execute". The project was set up in /home/miriki/projects/kdevelop/rheinwerk_openbook/hallo_x/.

Then I replaced the main.c content with another source from Rheinwerk OpenBook Linux Programmierung: 14.4.6 hallo_x.c (scroll a bit down, extended version of source) and added a new source my_delay.h into the project dir, too. The project still was built and linked without problems.

But the header file is used throughout various projects in the book and so I decided to create a "KDevelop global" include directory for this and other projects in the future in /home/miriki/projects/kdevelop/include. Then I added that directory under "Project - Open Configuration - Language Support - Includes/Imports" as a new entry. Additionally I inserted a new line into the CMakeLists.txt:
Code: Select all
target_link_libraries(hallo_x X11)

But the "Build" now errors out with
Code: Select all
/home/miriki/projects/kdevelop/rheinwerk_openbook/hallo_x/build> make -j4
Scanning dependencies of target hallo_x
[ 50%] Building C object CMakeFiles/hallo_x.dir/main.c.o
/home/miriki/projects/kdevelop/rheinwerk_openbook/hallo_x/main.c:9:10: fatal error: my_delay.h: No such file or directory
    9 | #include "my_delay.h"
      |          ^~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/hallo_x.dir/build.make:82: CMakeFiles/hallo_x.dir/main.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/hallo_x.dir/all] Error 2
make: *** [Makefile:149: all] Error 2
*** Failure: Exit code 2 ***

Code: Select all
miriki@lxdeb1130:~/projects/kdevelop/include$ ls -la
insgesamt 20
drwxr-xr-x 2 miriki miriki 4096  8. Jul 09:27 .
drwxr-xr-x 6 miriki miriki 4096  8. Jul 09:27 ..
-rw-r--r-- 1 miriki miriki  657  6. Jul 20:12 my_delay.h
-rw-r--r-- 1 miriki miriki 5233  3. Jul 18:21 vroot.h

So my questions actually would be:
a) Is the "Language Support" section the correct place to add the .h file(s)?
b) If yes, what went wrong? Otherwise, what else to do?
c) Is there a global include dir for KDevelop or do I have to set that up for each and every project again?
d) Same with libraries for the Linker: Every time into the CMakeLists.txt? Or is there a "global" place?

Michael
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS

Re: KDevelop and #include "xxx.h"

Sat Jul 09, 2022 12:59 pm
Hi!

If you write #include "my_delay.h" it expects the file in the current folder of the including file. Read it as "./my_delay.h". So either provide an absolute path "/home/miriki/projects/kdevelop/include/my_include.h" or use <> and "#include <my_delay.h>" like "<stdio.h>", etc. which should work if you added your include directory correctly.
If you write your own Makefiles, you must include this path yourself with -I /home/miriki/projects/kdevelop/include. (big i, not L or 1)
miriki
Registered Member
Posts
8
Karma
0
OS

Re: KDevelop and #include "xxx.h"

Sat Jul 09, 2022 2:11 pm
koffeinfriedhof wrote:If you write your own Makefiles, you must include this path yourself with -I /home/miriki/projects/kdevelop/include. (big i, not L or 1)

Thanks for your answer, but... forgive my dumbness ;-), but: How?

I compiled that source at the CLI according to the Rheinwerk Page:
Code: Select all
gcc -o hallo_x main.c -L/usr/X11R6/lib -lX11

(hallo_x.c changed to main.c)
But that was easy, of course, because the my_delay.h resided in the same / actual directory.
After moving the my_delay.h to the separate directory I used:
Code: Select all
gcc -o hallo_x main.c -I /home/miriki/projects/kdevelop/include/ -L/usr/X11R6/lib -lX11

and that worked with both using "" and <> at the #include.

But... changing into the Subdir "build" and call "make":
Code: Select all
[...] fatal error: my_delay.h: Datei oder Verzeichnis nicht gefunden [...]


And I thought setting that path in die "Language Support" would result in producing the needed -I /hom... extension for the command line or a line in the managed Make files or whatever. Or am I supposed to edit the build/Makefile? That's auto-generated, isn't it? There are settings for CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR in lines 70 and 73 though...

Michael
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS
The Makefile is generated from cmake (if you didn't change it). So you have to provide your include directory by changing CMakeLists.txt like
Code: Select all
target_include_directories(hallo_x PRIVATE ${YOUR_DIRECTORY})
set(SOURCES hallo_x.c another_file.c ${YOUR_DIRECTORY}/my_include.h)

The first adds the -I line, the second tells cmake to parse this file as part of your project.

I'm not that used to cmake as I still write Makefiles for C myself and only use it with Qt which needs other variables, but that should do it.
miriki
Registered Member
Posts
8
Karma
0
OS

Re: KDevelop and #include "xxx.h"

Sun Jul 10, 2022 12:04 am
koffeinfriedhof wrote:changing CMakeLists.txt like [...]

Yay, fully hit! I now have a file with:
Code: Select all
cmake_minimum_required( VERSION 3.0 )
project( hallo_x LANGUAGES C )
add_executable( hallo_x main.c )
target_include_directories( hallo_x PRIVATE ~/projects/kdevelop/include/ )
set( SOURCES hallo_x.c )
target_link_libraries( hallo_x X11 )
install( TARGETS hallo_x RUNTIME DESTINATION bin )

I just replaced the ${YOUR_DIRECTORY} parts with the real path of ~/projects/kdevelop/include/. Ah, and after first success I ommitted the 2nd source file in the set(SOURCES command. And now the "Build" produces (after a "Clear"):
Code: Select all
/home/miriki/projects/kdevelop/rheinwerk_openbook/hallo_x/build> make -j4
[ 50%] Building C object CMakeFiles/hallo_x.dir/main.c.o
[100%] Linking C executable hallo_x
[100%] Built target hallo_x
*** Finished ***

Using the ${xxx} way would require to set a variable with that value methinks? How would that be done?

And still the question: What is the point in setting the include path in the project configuration, in the ide settings that is?

Michael
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS

Re: KDevelop and #include "xxx.h"

Sun Jul 10, 2022 9:03 am
I think the language support is a global setting for the tools providing code completion, syntax and logical checking, etc. Depending on what you have installed (cppcheck, clang, …) you can set include folders individually. KDevelop is a modular tool and depends on what you did install and which Plugins you did enable. There are a lot of different tools like git, bazaar for versioning, valgrind, gdb, clang for code checking and debugging, etc.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]