Question

I am trying to compile a HAL API example on Netbeans. Netbeans shows warning and error about some header files. But I am able to compile my sample codes using following command line:

gcc `pkg-config --libs --cflags dbus-1 hal hal-storage dbus-glib-1 glib-2.0` main.c HalDemos.c HalDemos.h -o HalDemos -lpthread

How can apply this command to my Netbeans project?

Here the codes which I am trying to compile:

/* 
 * File:   HallDemos.h
 * Author: olcay
 *
 * Created on December 25, 2011, 5:05 AM
 */

#ifndef HALLDEMOS_H
#define HALLDEMOS_H

#ifdef  __cplusplus
extern "C" {
#endif

#include <hal/libhal.h>
#include <hal/libhal-storage.h>
#include <dbus/dbus.h>
#include <glib-1.2/glib.h>
//#include <dbus/dbus-glib-lowlevel.h>

    #define HAL_DBUS_SERVICE "org.freedesktop.Hal"
    #define HAL_ROOT_COMPUTER "/org/freedesktop/Hal/devices/computer"
    #define HAL_DBUS_INTERFACE_POWER "org.freedesktop.Hal.Device.SystemPowerManagement"

    static void handle_device_removed(LibHalContext *ctx, const char *udi);
    static void handle_device_added(LibHalContext *ctx, const char *udi);
    DBusConnection *connection;
    DBusError error;
    DBusMessage *mess;
    DBusMessage *reply;
    LibHalContext *ctx;
    LibHalDrive *drive;
    LibHalVolume *volume;
    const char *udi;
    int exit_code;

    int initHal();
    int getSystemInfo();
    int getDeviceWithCapability(const char *capability);
    void callbackLoop();
    void listDeviceContent();



#ifdef  __cplusplus
}
#endif

#endif  /* HALLDEMOS_H */




#include "HalDemos.h"
#include <stdio.h>

static void handle_device_removed(LibHalContext *ctx, const char *udi) {
    printf("Device with udi=%s is removed\n", udi);
}

static void handle_device_added(LibHalContext *ctx, const char *udi) {

    dbus_bool_t is_storage;

    is_storage = libhal_device_query_capability(ctx, udi, "storage", NULL);
    if (is_storage) {
        drive = libhal_drive_from_udi(ctx, udi);
        volume = libhal_volume_from_udi(ctx, udi);
        if (libhal_drive_is_hotpluggable(drive) || libhal_drive_uses_removable_media(drive)) {
            printf("Storage device added %s model %s\n",
                    libhal_drive_get_device_file(drive),
                    libhal_drive_get_model(drive));
            //printf("Mount point = %s\n", libhal_volume_get_mount_point(volume));
        }
        libhal_drive_free(drive);
    }

    //printf("Device with udi=%s is added\n", udi);
}

int initHal() {
    udi = "/org/freedesktop/Hal/devices/computer";
    dbus_error_init(&error);
    connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);

    if (dbus_error_is_set(&error)) {
        printf("Unable to connect to Dbus: %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }

    ctx = libhal_ctx_new();

    if (!libhal_ctx_set_dbus_connection(ctx, connection)) {
        printf("Error: %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }

    if (!libhal_ctx_init(ctx, &error)) {
        printf("Hal context initializing failure %s\n", error.message);
        return 1;
    }
}

int getSystemInfo() {

    char *kernel_version = libhal_device_get_property_string(ctx, udi, "system.kernel.version", &error);

    if (dbus_error_is_set(&error)) {
        printf("Error getting string property %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }

    char *power_management_type = libhal_device_get_property_string(ctx, udi, "power_management.type", &error);

    if (dbus_error_is_set(&error)) {
        printf("Error getting string property %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }

    dbus_bool_t can_hibernate = libhal_device_get_property_bool(ctx, udi, "power_management.can_hibernate", &error);

    if (dbus_error_is_set(&error)) {
        printf("Error getting bool property %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }

    printf("System information:\n");
    printf("Kernel                = %s\n", kernel_version);
    printf("Power management type = %s\n", power_management_type);
    printf("Hibernate             = %s\n", can_hibernate ? "Supported" : "Not supported");

    libhal_free_string(kernel_version);
    libhal_free_string(power_management_type);
    return 0;
}

int getDeviceWithCapability(const char* capability) {
    int num_devices = 0;
    char **udis = libhal_find_device_by_capability(ctx, capability, &num_devices, &error);

    if (dbus_error_is_set(&error)) {
        printf("Error getting bool property %s\n", error.message);
        dbus_error_free(&error);
        return 1;
    }
    if (num_devices == 0) {
        printf("No device found with input capability!");
        return 0;
    }
    int i;

    printf("Devices with input capability:\n");
    for (i = 0; udis[i]; i++) {
        printf("%2d - udi = %s\n", i + 1, udis[i]);
        /* Do something with it */
    }
    /* Free the string array */
    libhal_free_string_array(udis);
}

void callbackLoop() {
    GMainLoop *loop;
    loop = (GMainLoop*) g_main_loop_new(NULL, FALSE);
    dbus_connection_setup_with_g_main(connection, NULL);
    libhal_ctx_set_device_added(ctx, handle_device_added);
    libhal_ctx_set_device_removed(ctx, handle_device_removed);
    g_main_loop_run(loop);
}

void listDeviceContent(){

}

UPDATE:

Writing the command I used with $$() and adding it to additional compiler settings solved my problem.

Was it helpful?

Solution

Go to your project properties -> Build -> C Compiler -> Additional Options
Put your compiler flags here between $$() like:

$$(pkg-config --libs --cflags dbus-1 hal hal-storage dbus-glib-1 glib-2.0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top