Can't GTK3 programs be in multiple source files? - gcc

I am trying to make a C-program with multiple c-files in GTK3.
As models I am using:
how-to-split-a-c-program-into-multiple-files
and this
example-0.c
My program looks like this:
Functions.c (with the function activate)
#include "Functions.h"
#include <gtk/gtk.h>
#include "stdio.h"
static void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
}
Function.h (the header file)
#include <gtk/gtk.h>
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
static void activate (GtkApplication* app, gpointer user_data);
#endif
Main.c
#include "stdio.h"
#include <gtk/gtk.h>
#include "Functions.h"
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
When I compile, is says:
gcc -Wall `pkg-config --cflags gtk+-3.0` Functions.c Main.c `pkg-config --libs gtk+-3.0`
Functions.c:7:13: warning: ‘activate’ defined but not used [-Wunused-function]
static void activate (GtkApplication* app, gpointer user_data)
^
In file included from Main.c:4:0:
Functions.h:7:14: warning: ‘activate’ used but never defined
static void activate (GtkApplication* app, gpointer user_data);
^
/tmp/ccWzazr0.o: I funktionen "main":
Main.c:(.text+0x38): undefined reference to `activate'
collect2: error: ld returned 1 exit status
and is not compiled!

Yes, GTK programs can be split into multiple C files.
You get an error because you are telling your compiler that the function is only visible within that single C source file:
static void activate (GtkApplication* app, gpointer user_data)
If you want to use that function from another source file, you need to remove static keyword.
Both in the header file as well in the C file you need to remove it.

Related

how to fix undefined symbol for achitecture x86_64 error

This is my main code:
#include "startup.h"
int main(int argc, char *argv[]) {
printBanner(); <-- Undefined symbol for x86_64
readParameters(argc, argv); <-- Undefined symbol for x86_64
}
Contents for startup.h:
#ifndef STARTUP_H
#define STARTUP_H
#if defined __cplusplus
extern "C" {
#endif
void printBanner(void);
void readParameters(const int argc, char *argv[]);
#if defined _cplusplus
}
#endif
#endif
The implementations of the above mentioned functions is in startup.c:
#include "startup.h"
void readParameters(const int argc, char *argv[]) {
// code
}
void printBanner() {
// code
}
All the 3 files are within the same folder. Is there anything that is missing in terms of settings? I'm using xcode for this project.
I'm calling a function that is defined in .h from my main and the header is included in the .c file where functions are implemented. Can't see anything wrong with it.

Multiple definitions of init_module and cleanup_module

I am trying to compile multiple source files into a single module. I am having issues with multiple definition of functions.
Here is the code snippet of file1.c file
#include <linux/init.h>
#include <linux/module.h>
#include "headerfile.h"
#include <linux/slab.h>
static void swarm_init(void)
{
printk(KERN_ALERT "swarm_init function called\n");
}
void* func1(void) {
.....some code here
}
static void swarm_exit(void)
{
printk(KERN_ALERT "swarm_exit: exit function called");
}
module_init(swarm_init);
module_exit(swarm_exit);
Second file is as follows
#include <linux/init.h>
#include <linux/module.h>
#include "headerfile.h"
#include <linux/slab.h>
static void test_init(void)
{
printk(KERN_ALERT "Test init\n");
void *x;
x = func1();
}
static void test_exit(void)
{
printk(KERN_ALERT "test: exit function called");
}
module_init(test_init);
module_exit(test_exit);
headerfile.h is as follows
#ifndef _HEADERFILE_H
#define _HEADERFILE_H
typedef struct _hashmap_element{
int key;
int in_use;
void* data;
} hashmap_element;
typedef struct _hashmap_map{
int table_size;
int size;
hashmap_element *data;
} hashmap_map;
void *func1(void);
#endif
And my makefile is
obj-m :=myfile.o
myfile-objs := file1.o file2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I keep getting multiple definition of init_module and cleanup_module.
any idea whats going wrong?
Single module may have at most one initialization (declared with module_init() macro) and one cleanup (module_exit()) function. If you need initialization functionality for several parts of your module, you need to combine them manually into single initialization function. The same is true for cleanup.

gtk glade need help

I am using glade to make an user interface.
i have successfully generated the glade file
Now i have to include this file in my C code.
I am using following code:
#include <stdlib.h>
#include<gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWidget *builder,*window,*button;
gtk_init (&argc, &argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"shiv.glade",NULL);
window=GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
button=GTK_WIDGET (gtk_builder_get_object(builder,"button1"));
g_object_unref(G_OBJECT(builder));
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main ();
return 0;
}
My UI is a simple window having a button without any callback function.
I am getting following errors on execution
GTK-CRITICAL **: IA__gtk_widget_show assertion 'GTK_IS_WIDGET(widget)' failed
Change:
GtkWidget *builder,*window,*button;
with:
GtkWidget *window,*button;
GtkBuilder *builder;
this should fix.
Example:
#include <stdlib.h>
#include <gtk/gtk.h>
static void
close_window ( GtkWidget *widget, gpointer window)
{
printf("application close...\n");
gtk_widget_destroy((GtkWidget*)window);
}
int main (int argc, char *argv[])
{
GtkWidget *window, *button;
GtkBuilder *builder;
gtk_init (&argc, &argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"a.glade",NULL);
window = GTK_WIDGET (gtk_builder_get_object(builder,"window1")) ;
button = GTK_WIDGET (gtk_builder_get_object(builder,"button1"));
g_signal_connect (G_OBJECT (button), "clicked",G_CALLBACK (close_window),window);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window));
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all( window );
gtk_main ();
return 0;
}
From GTK3 reference manual:
GtkBuilder — Build an interface from an XML UI definition;
GtkWidget — Base class for all widgets

can't use static std::atomic (and don't know how to initialize it)

I have the following code:
#include <cstdlib>
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
//template<ATYPE TYPE>
//std::atomic<int> Object<TYPE>::counter = 0;
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
and when I compile it with the following command line:
clang++ -o test -std=c++11 -stdlib=libc++ test.cpp
I got the following error:
Undefined symbols for architecture x86_64:
"Object<(ATYPE)1>::counter", referenced from:
_main in testray-D4iTOH.o
Object<(ATYPE)1>::Object() in testray-D4iTOH.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no idea if what I am trying to do is technically possible. As the code hopefully shows, I am trying to create a static instance of the atomic class (BTW, I have no idea how to initialize this variable either. How do you intialize a static std::atomic<>?). What I am trying to do is count the number of instances of the class Object created while running the program, for each possible type (typeA, B, C, etc.).
That's the mechanism I came up with but maybe (beside the problem I have which I would like to fix if possible) someone could advice a better solution? It would be much appreciated.
Thank you so much.
As pointed by Dave in the comment, the static variable needs to be declared somewhere:
include
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
template<ATYPE TYPE>
std::atomic<int> Object<TYPE>::counter(0);
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
It compiles fine.

What is the exact equivalent to LD_PRELOAD on OSX?

I am using LD_PRELOAD to hook a library function, and in Linux it works perfectly. But I cannot figure out how to do the equivalent in OSX.
The setup I have on Linux is as follows:
The code is:
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>
void
rb_raise(unsigned long exc, const char *fmt, ...)
{
static void (*libruby_rb_raise)
(unsigned long exc, const char *fmt, ...) = NULL;
void * handle;
char * error;
if (!libruby_rb_raise) {
handle = dlopen("/path/to/libruby.so",
RTLD_LAZY);
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
}
libruby_rb_raise = dlsym(handle, "rb_raise");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}
}
// ...code...
return Qnil;
}
Which I then compile with:
gcc -Wall -O2 -fpic -shared -ldl -g -I/path/to/includes/ -o raise_shim.so raise_shim.c
I then execute with:
LD_PRELOAD=./raise_shim.so ruby
All of the above works well on Linux, what is the equivalent for each step to get this working on OSX? I have googled this and have not been able to get it to work with the information provided as the info for some of the steps are missing.
Thanks in advance!
Take a look at DYLD_INSERT_LIBRARIES. That's the variable you're looking for.
See also this answer.

Resources