First of all, I'm a begginer in C programming. I have been looking for a solution for a long time but I don't know what's going on with mi code or with Eclipse configuration. Basically, the problem occurs when I insert in the code a function from a external library. For some reason, eclipse is not able to debugg the code.
Let me explain that with a simple example:
Works incorrectly:
int main(void) {
char version[32];
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
En2version(version);
printf("Version %s \n", version);
return 0;
}
Works correctly:
int main(void) {
char version[32];
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
printf("Version %s \n", version);
return 0;
}
En2version() is the function took from the external library, and just eliminating it from the code it makes it work correctly.
I think the library is correctly linked to the project (there is no error when .lib is linked). Could you help me with some insight about what is happening?
Thank you all.
I don't see your #include
Whatever your are using, you need debugging symbols
either using the Program Database way if you have a .pdb
either using the Embedded Symbols way if you have a no .pdb
Related
I am currently trying to write a plugin backend in c by using .so files. Doing this in c works as I expect it to. However I thought about writing python plugins for my backend. Here is when i stumbled upon cython which seems to be very promising.
My backend is calling a function within the .so files and expects a value in return.
This function currently looks like this:
cdef public size_t transform_data(char *plugin_arguments, char **buffer):
printf("Entered function\n")
print("test\n")
printf("Test passed\n")
return 5
The interesting part is, that the printf works just fine. However the print doesn't. I suspect this is because there is some sort of linking error to a python module that I am missing? Also later on I would like to be able to add any python module to that file, for example the influxdb module. A call to influxdb.InfluxDBClient doesn't work either right now, I guess for the same reason that the print is not working.
I am compiling the file using
cythonize -3b some_plugin.pyx
and I have also tried to compile using a setup file that looks like this:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("some_plugin.pyx"))
both resulting to a segfault as soon as I hit the print call.
Here is the code that I am using to call the .so file:
#include "execute_plugin.h"
#include <Python.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
size_t execute_plugin(char file_name[FILE_NAME_SIZE], char *plugin_arguments,
char **output_buffer) {
if (!Py_IsInitialized()) {
Py_SetPythonHome(L"/home/flo/.local/lib/python3.8");
Py_SetPath(L"/usr/lib/python3.8");
Py_Initialize();
}
if (!Py_IsInitialized())
return 0;
void *plugin;
size_t (*func_transform_data)(char *plugin_arguments, char **output_buffer);
char path[PATH_SIZE];
if (!get_path_to_file(path, PATH_SIZE)) {
printf("Could not receive the correct path to the plugin %s\n", file_name);
return 0;
}
plugin = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
if (!plugin) {
fprintf(stderr, "Error: %s\n", dlerror());
fprintf(stderr, "Cannot load %s\n", file_name);
return 0;
}
func_transform_data =
(size_t(*)(char *plugin_arguments, char **output_buffer))dlsym(
plugin, "transform_data");
if (!func_transform_data) {
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(plugin);
return 0;
}
size_t length = func_transform_data(plugin_arguments, output_buffer);
printf("Size of answer is %ld\n", length);
dlclose(plugin);
Py_Finalize();
return length;
}
I have tried using the documentation and just copied the example: https://cython.readthedocs.io/en/latest/src/tutorial/embedding.html
In this example I didn't use an .so file but the .c and .h file that is also getting generated by the cythonize command. Interestingly enough the print function is working but as soon as I try to add another module like the influxdb module and try to call a function from it I also get errors.
Since I have not found a lot about using cython code in c I am wondering if what I am trying to do is even possible or if there is a better approach.
It's difficult to be certain what your issue is because you don't actually show how you call your Cython function. So what follows is a guess.
Cython does not produce standalone C functions. It produces functions that are part of a Python module and require that module to be initialized. What is probably happening is that Cython caches the lookup to the global print function as part of the module initialization. Since you skip the initialization the cached print function is not set up (hence the crash).
When you read the documentation, you'd have seen that all the examples ("Using Cython declarations from C" and "Embedding Cython modules...") import the module before using it. You must do this. The preferred way to do this is with PyImport_AppendInittab().
A second possibility is that you haven't initialized the Python interpreter. Again, this is not optional.
I ran a 4 line code and it compiled and linked without a hitch, but it refuses to print anything
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char* a = "book";
printf("%s\n", a);
return 0;
}
After compiling it and running the executable, nothing happens.
No error in the code.
Just write getch(); or getchar() before return 0;
to holding the output screen.
getch() or getchar() will hold the ouput screen for getting the user's input.
Works fine for me.
You've tagged this with terminal; if you are running it from the terminal, you should see some output, in my experience.
If you are running from an IDE,
keep the window open using Kapil K.'s answer;
keep the window open using an IDE setting, if there is one; or
find out where your IDE is putting the executable file, and run that from a terminal.
I hope this is no duplicate but I had no idea what to search for. I have a strange behavieour with a c program and a static variable.
About the program: I am configuring a serial port and change the serial file descriptor in a configure_tty(int *fd_ptr) function.The program is an application for an embedded linux running microblaze uclinux. I am programming and cross compiling under Ubuntu 14.
I stripped it down to the following example:
#include <stdio.h>
static int fd;
static void config_tty(int *fd_ptr);
static void config_tty(int *fd_ptr){
int desc = 5; // here was open(serial port)
*fd_ptr = desc;
}
void main(){
printf("before: %i\n", fd);
config_tty(&fd);
printf("after: %i\n", fd);
}
This gives me `before: 0` and then `after: 5` what is what I expected.
I have to mention that my original program is cross-compiled with gcc -89 parameter and the compiler for microblaze uclinux.
I found the problem thanks to gdbserver and gdb over TCP. Within the config_tty everything is fine but right after *fd_ptr = desc; fd didn't change.
So I tried different things and now comes my question:
Changing static int fd; to int fd; fixed it.
Can anyone tell me what is the reason for this and why it is no problem in my example on Codelite for Windows? Is it compiler-specific?
This question already has answers here:
C/C++ line number
(10 answers)
Closed 6 years ago.
Is there any way to get the source code line number in C code in run time?
The thought behind it is, suppose a software is made in plain C language. the .exe file is distributed to a user who knows nothing about C. Now if there is any error, the user can see the line number and report the error to the manufacturer so that the debugging can be done at manufacturer site. I just to debug the run time errors and get the corresponding line numbers in the source code while running it. I am a beginner in these stuffs.
If you are using the GCC compiler, you can use the Standard Predefined Macro - __LINE__ as a line number placeholder.
The compiler will fill in the line number while compiling.
Eg :-
printf("%d",__LINE__);
Use gdb instead. But I guess it will work:
if(someThingsWrong())
printf("wrong at line number %d in file %s\n", __LINE__, __FILE__);
You can use the built-in macros __LINE__ and __FILE__, which always expand to the current file name and line number.
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc<2)
{
printf("Error: invalid arguments (%s:%d)\n", __FILE__, __LINE__);
return 0;
}
printf("You said: %s\n", argv[1]);
return 0;
}
I am trying to learn C and I have just installed Xcode on my Mac. I wanted to run the first program that was already written
#include <stdio.h>
int main(int argc, const char * argv[])
{
// insert code here...
printf("Hello, World!\n");
return 0;
}
and got build failed.
I created a program in C. The libraries have been downloaded.
Thanks
Are you trying to learn C or Objective-C? As far as I know, XCode is only appropriate for Objective C.
Also, can you post your compiler error, that will help alot.