I am wanting to compile c through the command prompt on Windows.
I have the following file main.c:
#include <stdio.h>
#ifdef _WIN32
int main () {
printf("Windows\n");
return 0;
}
#else
int main () {
printf("*nix\n");
return 0;
}
#endif
And I follow these steps:
open cmd
$ gcc -o testprog.exe main.c -lWs2_32
$ testprog.exe
Output is *nix
I have installed MinGW and setup the path to gcc. I have also tried using __MINGW32__. What do I need to do?
Related
This is my simple ebpf program
#include <linux/bpf.h>
#include <linux/version.h>
#include <linux/ip.h>
#include <linux/if_ether.h>
#include <bpf_helpers.h>
#include <bpf_endian.h>// iproute specifically looks for this ELF section
//#include <net/sock.h>
#include <linux/bpf.h>
#include <linux/version.h>
#include <bpf_helpers.h>
#define bpf_printk(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), \
##__VA_ARGS__); \
})
SEC("kprobe/tcp_connect")
int connect(struct pt_regs *ctx)
{
bpf_printk("connect called -- Hello from [fawad] \n");
return 0;
}
char _license[] SEC("license") = "GPL";
compiling above program with this command
clang -O2 -Wall -target bpf -c -o xdp.o -I /build/root/usr/include/bpf -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ xdp.c
and this is a loader program
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include <bpf_load.h>
int main() { // raise the rlimit or see
// failed to create a map: 1 Operation not permitted
// when load_bpf_file is run
if (load_bpf_file("xdp.o"))
{
printf("%s\n", bpf_log_buf);
return 1;
}
while(1){
sleep(1);
}
} return 0;
}
compiling my ebpf loader program like this
clang -O2 -Wall -target bpf -c -o user.o -I /build/root/usr/include/bpf -I /home/ubuntu/Desktop/linux/test/linux-5.14.1/samples/bpf/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ -I /usr/src/linux-headers-5.11.0-41/arch/alpha/include/ user.c
When I run the loader program like #./user.o
it gives error
bash: ./user.o: Permission denied
running with sudo does not even recognized the file
root#this:/home/ubuntu/Desktop/ebpf# sudo ./user.o
sudo: ./user.o: command not found
root#this:/home/ubuntu/Desktop/ebpf#
Only your eBPF program needs to be compiled as eBPF, with the -t bpf target. Just compile your loader program regularly with either clang or gcc:
$ clang -Wall -o user user.c
Then you should be able to run your executable file after that (although I've not checked whether your program works as intended):
$ sudo ./user
(Note: You might still need to pass some of the include paths when compiling, -I /home/ubuntu/Desktop/linux/test/linux-5.14.1/samples/bpf/ for <bpf_load.h>, and maybe the path to <bpf/libbpf.h>)
For this question of mine, my goal was to create a software, main, that takes a plugin, libfunc.so, and libfunc.so would modify the value of burger.
main.c :
#include <stdio.h>
#include <stdlib.h> // for exit()
#include <dlfcn.h>
#include "main.h"
int burger = 3;
int main(){
void (*ptr_func)();
void *handle;
handle = dlopen("./libfunc.so", RTLD_NOW);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
*(void**)(&ptr_func) = dlsym(handle, "some_func");
if (!ptr_func){
fprintf(stderr, "%s\n", dlerror());
dlclose(handle);
exit(1);
}
printf("before ptr_func %d\n", burger);
ptr_func();
printf("after %d\n", burger);
return 0;
}
The declaration of burger,
main.h:
#ifndef MAIN_H__
#define MAIN_H__
extern int burger;
#endif
and the plugin [func.c] as this:
#include <stdio.h>
#include "main.h"
void some_func(){
burger += 10;
}
compiled all of these with clang and I got no error:
$ clang -rdynamic main.c -o main
$ clang -shared -fPIC func.c -o libfunc.so
But the problem arises with gcc:
$ gcc -rdynamic main.c -o main
$ gcc -shared -fPIC func.c -o libfunc.so
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc
vgKpEc.o:func.c:(.rdata$.refptr.burger[.refptr.burger]+0x0): undefined reference
to `burger'
Yes, I've tried removing the extern and it compiled successfully but the output was:
$ ./main.exe
before ptr_func 3
after 3
clang on the other hand compiled this successfully and working as I expected:
$ ./main
before ptr_func 3
after 13
The same as without extern compiled in clang
Do this two rivals really don't like to be consistent?
Here's the gist of it https://gist.github.com/harieamjari/8c816f39fe04d38d83022301872272ea
Additional notes:
The clang I have was from Termux. (an android application that simulates linux).
And the gcc I have was from cygwin.
gcc version 9.3.0
clang version 9.0.0
NOTICE
From my Termux, I installed gnu-8 and compiled the MWE above, but I do not experience an error.
Maybe I should use Windows.h for this matter instead of using dlfcn.h in cygwin.
I'm trying to debug claws-mail notification plugin, I have code like this:
#include "notification_indicator.h"
#include "notification_prefs.h"
#include "notification_core.h"
#include "folder.h"
#include "common/utils.h"
#include <messaging-menu.h>
#include <unity.h>
#define CLAWS_DESKTOP_FILE "claws-mail.desktop"
#include <stdio.h>
void main(void)
{
GList *cur_mb;
gint total_message_count;
total_message_count = 0;
/* check accounts for new/unread counts */
for(cur_mb = folder_get_list(); cur_mb; cur_mb = cur_mb->next) {
Folder *folder = cur_mb->data;
NotificationMsgCount count;
if(!folder->name) {
printf("Notification plugin: Warning: Ignoring unnamed mailbox in indicator applet\n");
continue;
}
gchar *id = folder->name;
notification_core_get_msg_count_of_foldername(folder->name, &count);
printf("%s: %d\n", folder->name, count.unread_msgs);
}
}
and I'm compiling it with this command:
gcc -I/home/kuba/Pobrane/claws-mail-3.13.2/src/
-I/usr/include/gtk-2.0/
-I/usr/include/cairo/
-I/usr/include/pango-1.0
-I/usr/lib/i386-linux-gnu/gtk-2.0/include/
-I/usr/include/gdk-pixbuf-2.0/
-I/usr/include/atk-1.0/
-I/home/kuba/Pobrane/claws-mail-3.13.2/src/common
-I/home/kuba/Pobrane/claws-mail-3.13.2/src/gtk
-I/usr/include/messaging-menu/
-I/usr/include/unity/unity/
-I/usr/include/dee-1.0/
-I/usr/include/libdbusmenu-glib-0.4/
-c `pkg-config --cflags glib-2.0` test.c
but gcc create object file test.o instead of a.out how can I create executable file? I'm running this on Xubuntu.
Remove the -c option from the commandline (which generates the object file instead of executable).
From man gcc:
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form
of an object file for each source file.
Examples:
To generate an object file (`.o' file):
gcc -c test.c
To generate an executable:
gcc test.c -o test
(if you omit the -o test, it'd generate a.out as executable by convention).
I am trying to compile the code:
#include <stdio.h>
#include <omp.h>
int main(){
#pragma omp parallel
{
printf("%d\t%d\n",omp_get_thread_num(), omp_get_num_threads());
}
return 0;
}
I tried both cc -o main.exe main.c and gcc -o main.exe main.c
Both ways I get "fatal error: 'omp.h' file not found"
So I downloaded the latest version of OpenMP. Then in the terminal in the directory of the downloaded folder I typed make and then
sudo cp ./libiomp5.dylib /usr/lib/
but I am still having the same issue. How can I get this to compile?
You should guard the Openmp include and function calls with #if _OPENMP in order to support compiling without the openmp option (gcc -fopenmp).
Is there any way I can compile a poorly designed header file to a object file without changing file extension or content using gcc, or do I have to copy the file/edit it? (This because I am using a public SDK, i.e. I do not have permission to edit the header file, and because using cp in my Makefile seems like a major hack, and time consuming too)
Example
main.c
#include <print.h>
#include <app.h>
int main(void) {
print("Starting app . . . ");
run();
}
app.h
#ifndef APP_H
#define APP_H
int runApp(void);
#endif
app.c
#include <print.h>
#include <app.h>
int runApp(void) {
print("This is my app!");
return 0
}
print.h
#ifndef PRINT_H
#define PRINT_H
int print(char* str) {
printf(str);
return 0;
}
#endif
Which is compiled using:
$ gcc -o main.o main.c
$ gcc -o app.o app.c
$ gcc -o main main.o app.o
The SDK example programs use a single object file (gcc -o main.o main.c & gcc -o main main.o), but that would just get really messy in my case.
Create
_print.h
int print(char* str);
print.cpp
#include <print.h>
and change your includes to "_print.h"