Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using C library in c++ code

59 views
Skip to first unread message

ag...@drrob1.com

unread,
Dec 10, 2014, 7:42:19 AM12/10/14
to
Hi. I would like to use the ncurses C library in my c++ code, on a
computer running Ubuntu 14.04 amd64

Is there anything special I have to look out for, or can I just use
the functions as documented from a c++ program?

I also learned to use make. Here is my makefile. Where do I put the
-lncurses ?

objects = tokenizec.o getcommandline.o timlibc.o hpcalcc.o rpnc.o
CXXFLAGS = -Wall -g -std=c++11

rpnc : $(objects)
g++ $(CXXFLAGS) -o rpnc $(objects)

rpnc.cpp : tokenizec.h macros.h getcommandline.h timlibc.h hpcalcc.h

tokenizec.cpp : tokenizec.h macros.h

getcommandline.cpp : macros.h

timlibc.cpp : timlibc.h macros.h

hpcalcc.cpp : hpcalcc.h macros.h


.PHONY : clean
clean :
-rm -v rpnc $(objects)

Thanks,
Rob

Paavo Helde

unread,
Dec 10, 2014, 12:06:22 PM12/10/14
to
ag...@drrob1.com wrote in news:gjfg8ahkvud9m67jsb405ktivt16u0qfv1@
4ax.com:

> Hi. I would like to use the ncurses C library in my c++ code, on a
> computer running Ubuntu 14.04 amd64
>
> Is there anything special I have to look out for, or can I just use
> the functions as documented from a c++ program?

There should be no problems. When compiling in C++, the C function
declarations must appear inside an 'extern "C"' block, but the ncurses
library headers have certainly already taken care about that.

>
> I also learned to use make. Here is my makefile. Where do I put the
> -lncurses ?

To the linker line, after $(objects) (order is important IIRC).

Jens Thoms Toerring

unread,
Dec 10, 2014, 2:22:36 PM12/10/14
to
In comp.lang.c++ Paavo Helde <myfir...@osa.pri.ee> wrote:
> ag...@drrob1.com wrote in news:gjfg8ahkvud9m67jsb405ktivt16u0qfv1@
> 4ax.com:
> > I also learned to use make. Here is my makefile. Where do I put the
> > -lncurses ?

> To the linker line, after $(objects) (order is important IIRC).

Definitely, libraries should always come at the end, otherwise
the linker may disregard functions defined in libraries that only
are only required by object files coming later.

> > tokenizec.cpp : tokenizec.h macros.h

These lines look a bit strange: the .cpp files don't "depend"
on the header files (i.e. they aren't to be generated anew
when one of the header files is changed). Instead you probably
meant here to have something like

tokenizec.o: tokenizec.cpp tokenizec.h macros.h

i.e. instruct make to rebuild tokenizec.o whenever any one of
tokenizec.cpp, tokenizec.h or macros.h was modified after the
.o was last built - what you have on te left side of the colon
is the "target", i.e. what make is supposed to produce, and
on the right side is the list of files the "target" is de-
pendent on (created from). Recompiling the .o file will then
force make to also relink the final executable due to the
changed .o file.

Without dpecifying how that's to be done make invokes the
default C++ compiler (for .cpp files) with your CXXFLAGS.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

ag...@drrob1.com

unread,
Dec 10, 2014, 9:22:37 PM12/10/14
to
On 10 Dec 2014 19:22:24 GMT, j...@toerring.de (Jens Thoms Toerring)
wrote:
It's interesting that my makefile works anyway.

Ian Collins

unread,
Dec 10, 2014, 9:53:07 PM12/10/14
to
> It's interesting that my makefile works anyway.

That's because (as Jens mentioned above) make is using its default rule
to make a .o from a .cpp. The only line doing anything is

rpnc : $(objects)

--
Ian Collins

ruben safir

unread,
Dec 11, 2014, 11:38:44 AM12/11/14
to
0 new messages