2011-12-26 122 views
0

我想在Netbeans上編譯一個HAL API示例。 Netbeans顯示關於某些頭文件的警告和錯誤。但我能夠使用下面的命令行編譯我的樣本代碼:如何在Neatbeans上編譯HAL示例?

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 

如何申請此命令我NetBeans項目?

在這裏,我正在試圖編譯代碼:

/* 
* 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:

寫我$$()用過的命令並將其添加額外的編譯器設置解決了我的問題。

回答

0

轉到您的項目屬性 - >建設 - > C編譯器 - >附加選項
把你的編譯器標誌在這裏$$()之間,如:

$$(pkg-config --libs --cflags dbus-1 hal hal-storage dbus-glib-1 glib-2.0)