How to compile LinuxCNC components with multiple .c and .h files

717 views
Skip to first unread message

Ricardo Nunes

unread,
Oct 9, 2014, 9:40:29 AM10/9/14
to machi...@googlegroups.com
I need to compile a LinuxCNC component that needs a lot of .c and .h files, how can I do it without pushing every code inside one .c file?

Schooner

unread,
Oct 9, 2014, 9:49:40 AM10/9/14
to Ricardo Nunes, Machinekit Mailing List
Put all the headers at the top of the first file and
#include nextfile.c at the end

You effectively have one long file but split up so it is manageable

Might or might not work with comp, but would if compiling a .c file

Other than that, you are looking at building with a makefile, which is
more complicated.

regards

On 09/10/14 14:40, Ricardo Nunes wrote:
> I need to compile a LinuxCNC component that needs a lot of .c and .h
> files, how can I do it without pushing every code inside one .c file?
> --
> website: http://www.machinekit.io blog: http://blog.machinekit.io
> github: https://github.com/machinekit
> ---
> You received this message because you are subscribed to the Google
> Groups "Machinekit" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to machinekit+...@googlegroups.com
> <mailto:machinekit+...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/machinekit.
> For more options, visit https://groups.google.com/d/optout.

Ricardo Nunes

unread,
Oct 10, 2014, 10:26:03 AM10/10/14
to Schooner, Machinekit Mailing List
I haven't tried it yet, but is that the only option? Using an "anti-standard" approach doesn't seem to be the solution here.

Can I compile it with GCC? If yes, where are the files that I need to include? Is there any special flags?

2014-10-09 15:49 GMT+01:00 Schooner <schoo...@tiscali.co.uk>:
Put all the headers at the top of the first file and
#include nextfile.c at the end

You effectively have one long file but split up so it is manageable

Might or might not work with comp, but would if compiling a .c file

Other than that, you are looking at building with a makefile, which is more complicated.

regards

On 09/10/14 14:40, Ricardo Nunes wrote:
I need to compile a LinuxCNC component that needs a lot of .c and .h files, how can I do it without pushing every code inside one .c file?
--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to machinekit+unsubscribe@googlegroups.com <mailto:machinekit+unsub...@googlegroups.com>.

Charles Steinkuehler

unread,
Oct 10, 2014, 10:28:43 AM10/10/14
to Ricardo Nunes, Schooner, Machinekit Mailing List
I'd just add your files to a Submakefile if you need to go beyond what
you can do with "comp".

On 10/10/2014 9:26 AM, Ricardo Nunes wrote:
> I haven't tried it yet, but is that the only option? Using an
> "anti-standard" approach doesn't seem to be the solution here.
>
> Can I compile it with GCC? If yes, where are the files that I need to
> include? Is there any special flags?
>
> 2014-10-09 15:49 GMT+01:00 Schooner <schoo...@tiscali.co.uk>:
>
>> Put all the headers at the top of the first file and
>> #include nextfile.c at the end
>>
>> You effectively have one long file but split up so it is manageable
>>
>> Might or might not work with comp, but would if compiling a .c file
>>
>> Other than that, you are looking at building with a makefile, which is
>> more complicated.
>>
>> regards
>>
>> On 09/10/14 14:40, Ricardo Nunes wrote:
>>
>>> I need to compile a LinuxCNC component that needs a lot of .c and .h
>>> files, how can I do it without pushing every code inside one .c file?
>>> --
>>> website: http://www.machinekit.io blog: http://blog.machinekit.io
>>> github: https://github.com/machinekit
>>> ---
>>> You received this message because you are subscribed to the Google Groups
>>> "Machinekit" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to machinekit+...@googlegroups.com <mailto:machinekit+
--
Charles Steinkuehler
cha...@steinkuehler.net

signature.asc

John Morris

unread,
Oct 10, 2014, 11:29:16 AM10/10/14
to Ricardo Nunes, Machinekit Mailing List
Ricardo,

Have you tried running 'comp' with multiple input files on the command
line? I guess it tries to make one component from each input?

There should be some way to compile a multiple-source component out of
tree, but I haven't heard of anyone actually doing it before.

For a single source file component in template form, the 'comp' script
(you can read it) renders a .comp template into C, and then sets up a
Makefile that includes 'Makefile.inc'.

If you've written your component as a .comp template, it'll have to be
converted to C first. Try 'comp --preprocess file1.comp file2.comp'.

Write a Makefile that looks something like this for a real-time
component (see the 'build_rt' function in 'comp'; userland components
are different):

obj-m += mycomp
include /usr/share/linuxcnc/Makefile.modinc
mycomp-objs = file1.o file2.o

Then 'make modules' (or optionally 'make modules install'). You may
have to tell it the thread style, e.g. 'make threads=xenomai modules'.

The above is untested, pure speculation, and you're in uncharted
territory. Point us to your code for better help, and keep us in the
loop about how it goes. This would make a great tutorial for folks
writing more complicated out-of-tree components.

John

Schooner

unread,
Oct 10, 2014, 11:42:28 AM10/10/14
to Machinekit Mailing List
Hi

I compile Qt based hal components all the time, which contain multiple
C++ files and headers, so use a .pro file to create a Makefile

This is an example, with all the basic defines and libs, you will need
to adapt to your specific paths

TEMPLATE = app
TARGET = qtvcp
DEPENDPATH += .

QMAKE_CXXFLAGS += -D_FORTIFY_SOURCE=0 -D_GNU_SOURCE -DULAPI -mhard-float
-fno-fast-math -mieee-fp -fno-unsafe-math-optimizations
-Wframe-larger-than=2560 -Os -std=c++0x
INCLUDEPATH += . /usr/include usr/include/linux
/usr/src/machinekit/include /opt/Qt/5.3/gcc/include
/opt/Qt/5.3/gcc/include/QtDesigner
QMAKE_LIBDIR += /lib /usr/src/machinekit/lib
/opt/Qt/5.2.1/gcc_64/plugins/designer
LIBS += -llinuxcncini -llinuxcnchal -lnml -llinuxcnc -lposemath
-lhalwidgets_qt5

QT += core gui widgets

# Input
HEADERS += qtvcp.h HAL_Access.h NML_Access.h
FORMS += qtvcp.ui
SOURCES += main.cpp qtvcp.cpp qtvcp_funcs.cpp qtReceive.cpp
HAL_Access.cpp HAL_Access_Local.cpp NML_Access.cpp
NML_Access_Machine_Commands.cpp

Alternately, doing commandline compilation of components based on a C
file looks something like this

gcc -Os -g -I. -I/usr/realtime/include -I.
-I/usr/src/linuxcnc-dev-rtai/include -I/usr/include/modbus \
-D_FORTIFY_SOURCE=0 \
-mhard-float -DRTAI=3 -fno-fast-math -mieee-fp
-fno-unsafe-math-optimizations -DRTAPI -D_GNU_SOURCE -Drealtime \
-D_FORTIFY_SOURCE=0 -D__MODULE__ -I/usr/include/linuxcnc
-Wframe-larger-than=2560 -URTAPI -U__MODULE__ -DULAPI -Os \
-o {output file name} {filename.c} -Wl,-rpath,/lib -L/lib
-L/usr/lib -L/usr/src/linuxcnc-dev-rtai/lib -llinuxcnchal -llinuxcnc
-lmodbus ;;
(This of course was for linuxcnc on 10.04)

Between the 2 you should get a picture of the includes, defs and libs
required and adapt accordingly

regards

Florian Kerle

unread,
Sep 4, 2015, 3:53:31 AM9/4/15
to Machinekit
Dear Schooner,

we've used to develop HAL comps either via
- halcompile using C
- using C++, taking care of realtime stuff ourselves.

Are you able to compile C++ with halcompile, and if, would you kindly share your config?

Best regards,
Florian

schoo...@btinternet.com

unread,
Sep 4, 2015, 4:43:35 AM9/4/15
to Florian Kerle, Machinekit
Hi

No, comp (has not been changed to halcompile in Machinekit) will compile C, rt and userspace standard components.

instcomp will compile 2 types of instantiated C components of rt type only at this stage (2nd type in development still)

Co-incidentally I am currently writing C++ components for Machinekit, but only userspace ones at present.

Your approaches are both valid still, I do both.
So long as you are just making C calls within a C++ class, the underlying rt library functions can be used.

The problem I have not solved at present, is passing a C++ member function address outside the class as a callback,
without making the function an external static one, which defeats the object of using C++

The compiler script for compiling C++ userspace components will look something like this (paths adjusted as required)

g++ -Os -g -I. -I/usr/src/machinekit/include -I/usr/local/include -D_FORTIFY_SOURCE=0 \
    -mhard-float -fno-fast-math -mieee-fp -fno-unsafe-math-optimizations -D_GNU_SOURCE \
    -Wframe-larger-than=2560 -DULAPI -Os \
     -o $1 $1.cpp -Wl,-rpath,/lib -L/lib -L/usr/src/machinekit/lib -llinuxcncini -llinuxcnchal -lnml -llinuxcnc -lposemath

where $1 is the name of the file to be built without extension

Hopefully that is of some help

regards
To unsubscribe from this group and stop receiving emails from it, send an email to machinekit+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages