Welcome new user! You can search existing questions and answers without registering, but please register to post new questions and receive answers. Note that due to large amounts of spam attempts, your first three posts will be manually moderated, so please be patient.
Because of un-manageable amounts of spam despite our use of CAPTCHAs, email authorization, and other tools, we have discontinued this forum (see the 700k+ registered users with validated email addresses at right?). Please email us any questions or post bug reports and feature requests on GitHub at https://github.com/jevois -- The content below remains available for future reference.
Welcome to JeVois Tech Zone, where you can ask questions and receive answers from other members of the community.

Include cpp files in compiling module?

0 votes
For a jevois independent project, I have the following line in my CMakeLists.txt to instruct what cpp files to include in compiling the code.

add_executable(${PROJECT_NAME}_node src/[ModuleName].C src/[IncludeFile].cpp)

[IncludeFile].C and [ModuleName].cpp also both have the following line:

#include "[IncludeFile].h"

1. Where would be the best place to add the [IncludeFile].h? I'm currently using an absolute path for the compiler telling where the .h file is, e.g. "/home/[ModuleName]/include/[IncludeFile].h".

2. How can I edit the CMakeLists.txt from the SampleModule to make sure the appropriate sources are included in compiling? The only reference to src files I see is the following line:

jevois_setup_modules(src/Modules "")

Any clarification would be greatly appreciated.
asked May 9, 2018 in Programmer Questions by unswniarc (360 points)

1 Answer

+1 vote
 
Best answer

Yes, that's a tricky one! JeVois modules are compiled as shared libraries (.so) files so that they can be loaded/unloaded at runtime. So the compilation directives are a bit different than a regular executable.

jevois_setup_modules(dir link_dependencies) 

creates a CMake target for each sub-directory under "dir", with target name equal to the module directory name. In the SampleModule skeleton, when "dir" is "src/Modules" there is only one subdir, "SampleModule", so you get only one target, also named SampleModule.

then for this target, we do an add_library() as part as the jevois_setup_module() macro, with the SampleModule.C file as source for it

from there, if you want to let CMake know about your include files, you would use

include_directories(dirname) 

where dirname is where your .h files are. I believe "." is already included by default so you should not have to do it explicitly. In any case, letting CMake know about includes is only useful if you are going to do partial recompilation (type "make" from inside hbuild/ or pbuild/) and you want the Makefile generated by CMake to know which cpp files to recompile when some of your .h files have changed. If you just use our scripts rebuild-host.sh and rebuild-platform.sh you would usually not care about correct source/include dependencies.

Now to add more cpp files, you would typically do

target_sources(SampleModule PRIVATE otherfile.cpp)

You can specify more than one cpp files, just list them all separated by spaces.

Note that I have not tested this right now and I am no CMake expert. If I remember well, the PRIVATE flag is needed to that the generated .o for each of your extra sources gets linked into the .so of your module.

You may want to check out these extra resources:

https://github.com/jevois/jevois/blob/master/CMakeModules/JeVois.cmake - defines jevois_setup_modules()

https://github.com/jevois/jevoisbase/blob/master/CMakeLists.txt - in jevoisbase, we build libjevoisbase.so and add all sorts of contributed files, includes, compiler flags, link dependencies, etc. The CMake target name in that file is jevoisbase, and you would replace that by your module name.

answered May 10, 2018 by JeVois (46,580 points)
selected May 11, 2018 by unswniarc
...