Reputation: 2671
My project has a function that clears the terminal, which is implemented using the curses library. Compiling with the -lcurses
flag works fine, but compiling without yeilds
/tmp/cc3T2MVI.o: In function `ClearScreen()':
clear_term.cpp:(.text+0xb): undefined reference to `cur_term'
clear_term.cpp:(.text+0x26): undefined reference to `setupterm'
clear_term.cpp:(.text+0x37): undefined reference to `tigetstr'
clear_term.cpp:(.text+0x3f): undefined reference to `putp'
collect2: ld returned 1 exit status
This is obviously expected because it cant find the library, but because this functionality is supplemental it would be preferable to define ClearScreen()
as an empty function than to have compilation fail. I know that I put the function definition in a #ifdef
block but I don't know any flags defined by curses.
Is it possible to catch these errors and instead define ClearScreen()
as an empty function?
Upvotes: 2
Views: 483
Reputation: 98118
You can define a macro in the Makefile:
use_curses=1
FLAGS+=-DUSING_MAKEFILE
ifeq ($(use_curses),0)
FLAGS+=-DNO_NCURSES
else
LIBS+=-lcurses
endif
And in the code:
#ifndef USING_MAKEFILE
# error "Please use provided Makefile to compile!"
#endif
#ifdef NO_CURSES
void ClearScreen() { }
#endif
Upvotes: 1
Reputation: 76795
The problem you haven't considered is that your code probably #include's ncurses.h, which won't ever work without the library being installed where the compiler can find it.
Upvotes: 0
Reputation: 120229
Your build script should detect whether a suitable version of curses is present on the build machine. You can generate such script with GNU Autotools for example (the result iwould be a familiar configure
script. You can also write a simple custom script insh
/bash
.
Upvotes: 0
Reputation: 15642
What you are trying to do (configuring the project with respect to dependencies) — is the classical task of build systems.
For example, with CMake, you'll have FindCurses
module, which defines CURSES_FOUND
preprocessor variable (if it founds the library).
With GNU Autotools you'll have similar macro, consult the relevant documentation.
If you're using your own build system — then you have to manually code the handling of relevant flags in configuration time.
Upvotes: 0
Reputation: 495
You really need this library. Maybe it will help you: http://linux.die.net/man/3/tigetstr
Upvotes: 0
Reputation: 41262
Actually, that is a linker error. And no, it can't really be caught at compile time. But one possibility would be to load the shared object dynamically in ClearScreen
. If it failed to load the library, then it could just return. Doing the check at run time may be preferable to build time checks if you are not guaranteed to be building the binary on the target system on which it will ultimately be running.
Upvotes: 0