我正在尝试在Netbeans上编译HAL API示例。 Netbeans显示有关某些头文件的警告和错误。但我可以使用以下命令行编译我的示例代码:
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(){
}
更新
编写我与$$()
一起使用的命令并将其添加到其他编译器设置解决了我的问题。
答案 0 :(得分:0)
转到您的项目属性 - &gt;构建 - &gt; C编译器 - &gt;附加选项
将编译器标志放在$$()
之间,如:
$$(pkg-config --libs --cflags dbus-1 hal hal-storage dbus-glib-1 glib-2.0)