2

Ok, so I'm trying to find if there is an existant declaration of a function in main.cpp with CMake, but CMake doesn't want to find it.

So far I've tried this:

include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(draw DRAWFUN)

this

include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS(main.cpp draw main.cpp DRAWFuN)

this

include(CheckSymbolExists)
CHECK_SYMBOL_EXISTS(draw main.cpp DRAWFUN)

and lastly this

include(CheckCXXSymbolExists)
CHECK_CXX_SYMBOL_EXISTS(draw main.cpp DRAWFUN)

but as I've mentioned before, none of those work. All they say is

-- Looking for draw
-- Looking for draw - not found
-- Configuring done
-- Generating done

So my question is what am I doing wrong and how can I get it working?

Thanks in advance

EDIT: main.cpp file:

#include "P5c.h"    

void setup(){
    size(800, 600);
    background(51);
}

void draw(){
    background(51);
}
krentm
  • 132
  • 12
  • From cmake docs: for [`CheckFunctionExists`](https://cmake.org/cmake/help/latest/module/CheckFunctionExists.html) "checks that the is provided by libraries on the system", so clearly the first won't work. I'm unsure why `CheckSymbolExists` doesn't work, from [its docs](https://cmake.org/cmake/help/latest/module/CheckSymbolExists.html). It might help if you had a sample `main.cpp` file. – Justin Nov 02 '17 at 08:04
  • `CHECK_FUNCTION_EXISTS` only checks that non-mangled function name is present in export table of some compiled binary. It does not check whether some function is present in some source file. – user7860670 Nov 02 '17 at 08:06
  • @Justin I'm adding main.cpp example file – krentm Nov 02 '17 at 08:07
  • @VTT so it's not going to be useful for my application? – krentm Nov 02 '17 at 08:09
  • I don't have enough knowledge in CMake to know how to solve this, but you probably need to use [`CheckCXXSymbolExists`](https://cmake.org/cmake/help/latest/module/CheckCXXSymbolExists.html), but maybe you could figure out a way to use [`CheckCXXSourceCompiles`](https://cmake.org/cmake/help/latest/module/CheckCXXSourceCompiles.html). I hope someone can figure this out :-) – Justin Nov 02 '17 at 08:11
  • I don't know how could it be useful even if it worked as you expected. As I understand the point of `CHECK_LIBRARY_EXISTS` is to identify some missing system functions so project code could supply alternatives (kinda autotools-style). What are you actually trying to accomplish by calling `CHECK_LIBRARY_EXISTS`? – user7860670 Nov 02 '17 at 08:12
  • @VTT So, I'm working on function calling from header, so I need to check if the function I'm calling exists in main, if it doesn't I want to comment that part of the code out, because It's not going to be useful and it's just going to waste space. – krentm Nov 02 '17 at 08:16
  • @Justin thanks for the help :D – krentm Nov 02 '17 at 08:17
  • If you try to call a function that does not exists then there will be a compilation error so there is no need to manually check anything. Also commenting code out is not a good practice. – user7860670 Nov 02 '17 at 08:20
  • @VTT Ok, I'm going to try to explain it again... I don't want users to change header I'm working or nor I want them to have to write functions with empty bodies if they don't need it. So I'm trying to do it with CMake, so it would skip over that part calling that function if there is no such function. AFAIK if I skip that part that's calling non-existant function there will be no complication, because there will be nothing calling non-existant function, right? – krentm Nov 02 '17 at 08:26
  • `configure_file()` might be useful for you, take a look at it. – arrowd Nov 02 '17 at 08:29
  • @arrowd Could I use this to rename functions? – krentm Nov 02 '17 at 08:32
  • 3
    To be honest this does not make any sense. If you are making a library then a header file that users of this library will need to include should only contain declarations of functions exported from your library. And there is no need for users to change that header or write functions with empty bodies (unless library itself is flawed in some way). – user7860670 Nov 02 '17 at 08:33
  • @VTT ok, I'm new to the c/c++ header programming, but is there another way to call function in main from header? You know... I'm trying to make an event system, so if I have a function in main called draw(), I would want it to get triggered once every second, so I'd call it from inside of the header file. so all the user will have to write is ```void draw(){/*do something*/}``` and the include, but the user won't have to implement the whole timer thingy and everything else? – krentm Nov 02 '17 at 08:36
  • 1
    The better approach for event system would be to accept the function that should be called periodically as a parameter from user. For example: `#include void UpdateLoop(::std::function update) {while(update()) { sleep(1000); } }` This way user will be able to pass any compatible function or a callable object instead of being forced to write function named strictly `draw`. And there is no need for any fancy cmake checks. – user7860670 Nov 02 '17 at 08:48
  • @VTT that's pretty much what I have now, but I really wanted to prevent user from having to set those function calls. You might've heard of Unity3D, well their JS has two functions called ```update()``` and ```start()```, so that's kinda what I'm aiming for. I prefer ease of use(of the library/API) above ease of production(of the library/API). – krentm Nov 02 '17 at 08:55
  • @VTT and also, I don't think that would be smart, because sleep would be blocking program from running anything else in the mean time. – krentm Nov 02 '17 at 09:04
  • Well, C++ is certainly different from Unity3D scripting system. However the proper analogue for hooking up `update()` function from script in C++ would be to let user implement an interface with virtual `update()` method and accept this interface as a parameter because the script is parsed with an export table containing `update` function built. Anyway, approach with requiring user to write functions with some hard-coded names in some hard-coded locations (and then doing some trickery if user does not provide them) is definitely unusual for c++. PS I've used `sleep` only to wrap up short code. – user7860670 Nov 02 '17 at 09:14
  • @VTT so it's impossible, and I must use function pointers with setters for that pointers passing wanted functions? No other way... And just when I thought that I'm going to make my API easier to use... – krentm Nov 02 '17 at 09:17
  • You may want to check how some existing C++ event systems are made, for example [FLTK simple animation](https://stackoverflow.com/questions/10786612/fltk-simple-animation). Callback is registered using `Fl::add_timeout(2,winUpdate,window);` call. Second parameter is a function pointer, third is some user context data pointer (window in this case). – user7860670 Nov 02 '17 at 09:33
  • Will take a look at it. Thanks :D – krentm Nov 02 '17 at 09:34
  • @VTT - This is relevant to a use case of mine. Fedora purposely removes certain functions from OpenSSL because of patents. These are optimized versions of more general functions that do exist. I want to make something compile on any Linux distribution. So I want to decide what code to compile based on whether a certain function is available in OpenSSL. How do I do this? – Omnifarious Aug 09 '18 at 19:08
  • @Omnifarious Don't they provide some sort of conditional compilation macro to detect such a situation? Writing detection tests is typically a corner situation that should be avoided. – user7860670 Aug 11 '18 at 13:16
  • @VTT - I didn't know there was such a macro when I wrote that comment. But I do now. – Omnifarious Aug 11 '18 at 18:47

0 Answers0