Packing some of OpenGL commands in a dynamic library, got "wglMakeCurrent failed" - c

I am trying to put some of my OpenGL commands in an external library. To be specific, I'm currently drawing a 3d scene with Tcl language, and now I want to put some openGl functions (drawing functions for example) in a dll (programmed in C) and load it from Tcl. This is because I will have large quantity of data to render. With a compiled C library it will run faster.
However, I'm having problem doing so. When I launch my program, a "wglMakeCurrent failed" error occurs just before the second run of my display callback function.
Both Tcl and C parts report the same thread ID. So I suppose that I do not have to worry about multithreading and context sharing issue.
This is my display callback function:
proc DisplayCallback { toglwin } {
# adjust camera and objects ...
if {[catch {set cubeList [ExternalDrawCube $::cube(size)]}]} {
puts "Catch drawcube error !!"
} else {
glCallList $cubeList
}
$toglwin swapbuffers
}
Any help or suggestions?

I finally found the stupid bug I did in my code..
In the C function, I wrote "glEnd" instead of "glEnd()". I forgot it when converting the code from tcl!
Hopefully this would help people who have similar wglMakeCurrent failed error...
Good debugging for all

Related

I cant use auxiliary functions from zephyr ( tty.h) file

I am building a project and I have the board stm nucleo_l496zg. All i want to do is to use the drivers from the board in order to communicate the board after west flash with minicom, it is a simple string transfer and response program. I am building this project with zephyr and my issue is that I cant use the functions tty_init , tty_read and tty_set_rx_buf despite that I use the proper include " #include <console/tty.h> ". The compiler returns an undefined reference to thoose three functions but in my program I am using another one function from tty.h header which is tty_set_timeout but at this function it doesnt say nothing. Though I notiched in that in here(documentation of tty.h) tty_Set_timeout is the only function that has something inside. I cant understand why I am getting that please if someone can help me let me know !
I had the same problem and I solve it by adding those lines to my .conf file:
CONFIG_SERIAL=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
And don't forget to mention the file (.conf file) also in your CMakeList.txt file:
set(CONF_FILE ***.conf)
Hope that it will work with you as well

dll loaded with LoadLibrary() is not being accessed, shows the error "dll was not found"

I'm a something of a beginner in programming. Here's a snippet from a C Program I wrote for Windows, trying to use NCurses library, compiled with mingw32.
char NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
char *dllDirectory = malloc(strlen(NPath) + strlen("\\dlls") + 1);
strcpy(dllDirectory, NPath);
strcat(dllDirectory, "\\dlls");
SetDllDirectoryA(dllDirectory);
HINSTANCE hGetProcIDDLL = LoadLibrary("libncursesw6.dll");
if (!hGetProcIDDLL) {
printf("libncursesw6.dll could not be loaded!");
return -1;
} else {
printf("loaded libncursesw6.dll\n");
}
I tried to load the libncursesw6.dll from the dlls subfolder, from the same folder of the executable.
The above code works perfectly and displays, the dll is loaded.
But when I try to use use the library functions, I get an error box telling me libncursesw6.dll was not found. The program then works fine if is place the dll with the executable (which is what I'm trying to avoid).
When the following lines are added after the previous snippet, I get a run time error.
initscr();
addstr("Hello World");
refresh();
getch();
endwin();
I've included the necessary header files and got no compilation errors or warnings.
Am I doing something wrong??
The problem is, that you load the library after you have started the program, but if you use the functions in the code, the runtime linker, that tries to resolve unresolved symbols, needs to find the library before it passes control to the actual program.
So, you cannot do it this way. It would be possible to not use the ncurses functions directly, but to define a bunch of function pointers and resolve the symbols you need manually with GetProcAddress, but this is quite cumbersome.
For example like this:
void (*initscr)() = GetProcAddress(hGetProcIDDLL, "initscr");
Another possibility is, to link the ncurses library statically, so it isn't needed at runtime.

R, SQL problems debugging a package, debug(.Call)

I am trying to debug an error that I think is in a pacakge. While I am stepping through the code with the debugger there is a section of code that produces the error I am getting.
if (fetch == FALSE | nrow(data) < 1) {
stat <- .Call("RODBCExecute", attr(channel, "handle_ptr"),
data, as.integer(rows_at_time))
if (stat == -1L) {
if (errors) {
stop(paste0(RODBC::odbcGetErrMsg(channel), collapse = "\n"))
}
else {
return(stat)
}
}
.Call() sets stat = -1 and this produces the error. I tried to use debug(.Call) but this is not working. From what I understand from searching online is that the .Call() uses compiled C/C++ code. Is there anyway to debug this code any farther?
You are correct: .Call("thisCFunction",...) calls a C/C++ function, loaded in by the package from the .so file, that is called thisCFunction in it's C++ declaration.
I would not try to debug this; you would have to go through the C++ source code by hand to figure out exactly what's happening, which would require a firm understanding of not only general C/C++ but also R's API (and the author's style of programming). Not trying to be rude, but I would guess if you are unfamiliar with .Call and what it does, you're probably unfamiliar with R's API.
An easier first step would be to track down exactly what is being supplied to this function and try to figure out why it's not what RODBCExecute expects. Given that the authors have written an error message, I would check to see if that error message helps point out the issue. My guess is that the real error occurred much earlier and this results in providing invalid arguments to RODBCExecute.

symbol lookup error on a command

i'm trying to do some code in a keyboard driver, a 3rd party software that looks like this can run the command i'm trying to do in a plugin file that compiles alongside the daemon that the command needs to be sent to. the command looks like this.
g15_send_cmd (g15screen_fd,G15DAEMON_MKEYLEDS,mled_state);
here's the code i'm working with and trying to run the command in (it compiles as a plugin with the daemon. in the uncompiled source it's
"g15daemon/plugin/g15_plugin_uinput.c"
the file that defines the command is in
(link)
"g15daemon/libg15daemon_client/g15daemon_clinet.h"
whereas with the g15macro (3rd software) is run from outside the daemon for various reasons i don't want to (and pretty much can't) use it, one being speed of execution of commands when keys are pressed.
so the program compiles like this without error it seems. but if the code i specified above activates, the driver(daemon) crashes giving
g15daemon: symbol lookup error:
/usr/lib/g15daemon/1.9.5.3/plugins/g15plugin_uinput.so: undefined
symbol: g15_send_cmd
what did i do wrong or what am i missing here? (and i'm sorry if the code in the plugin file is ugly down by that switch command, but i didn't know how to do any better since i don't know C much at all, all i have behind me are Py and C#)
Edit: the solution was given
but i don't know how to add a linker flag, also since it links to a part of the program being compiled will it even work?
You forgot to link your plugin with g15daemon_client library. The simple way to fix it is to add -lg15daemon_client to the linker flags.

How to use a C library from D?

Today I heard about the D programming and that it is compatible to C code. Nevertheless I haven't found any information on whether it is possible to use C libraries like GTK or PortAudio from D?
If it is possible, could you explain how to do this?
It is possible to call C libraries from D. What you need to do is to convert the C header files to D. For the most part this is pretty straightforward, and there is a hard-to-use command-line tool to help automate the process. It's never really worked for me on anything but toy examples, but it could be a good start to see the kind of transformations that need to be done. Just put a snippet you're having trouble translating into a header by itself and see what htod does with it.
The biggest problem you'll usually encounter is creative use of the C preprocessor. Some things can be turned into version() statements in D, but not all.
As for actually compiling and linking with the code, on unix-like platforms I think you can compile and link in the C code using GCC. On Windows you either have to compile the C files using DMC and link with DMD. Or you can compile the C code into a DLL using any compiler capable of that, and then to link with DMD you need to make a DMD-compatible import lib out of the DLL. This can be done using the implib tool found in the free Basic Utilities Package available from DigitalMars.
There are also a lot of these header translations have already been done. It's useful to browse the Bindings project of Dsource first, or ask on the digitalmars D newsgroups first before embarking on something big like translating GTK headers. A lot of popular libraries like GTK have already been wrapped (e.g. here: GTKD)
D code can be linked with C object files, and can interact with C dlls, but you'll need to generate a D module from the C header file you want to use. The official D website has a guide for doing that very thing.
Popular alternative is to load the library during the run-time. Here is an example how to load libpng and call a libpng function:
module libpngtest;
import std.stdio;
import core.sys.posix.dlfcn;
alias uint function() png_access_version_number_t;
int main() {
auto lib = dlopen("libpng.so".ptr, RTLD_LAZY | RTLD_LOCAL);
if (lib is null) {
writeln("EEEK!");
writeln(to!string(dlerror()));
return -1;
} else {
writeln("WOOT!");
auto png_access_version_number = cast(png_access_version_number_t)dlsym(lib, "png_access_version_number");
writeln(png_access_version_number());
}
if (dlclose(lib) == 0) {
return 0;
} else {
return -1;
}
} // main() function
// compile: dmd libpngtest.d -L-ldl
// run: ./libpngtest
Use the DPaste to test it: http://www.dpaste.dzfl.pl/917bc3fb
You need to write C bindings.
This answer explain how.
Take a look at http://dsource.org
There are many projects that might help you to get start with

Resources