Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/bash
cat > Makefile <<EOF
APPNAME=test_struct_different_decl
CFLAGS=-g2 -O0 -Wall -Wextra -Wpedantic
CC=clang
all:
\$(CC) \$(CFLAGS) -c -o mod1.o mod1.c
\$(CC) \$(CFLAGS) -c -o mod2.o mod2.c
\$(CC) \$(CFLAGS) -o \$(APPNAME) mod1.o mod2.o main.c
clean:
rm *.o \$(APPNAME) || true
EOF
cat > common_hdr.h <<EOF
#pragma once
struct test_struct;
struct test_struct *alloc_test_struct(void);
void mod1_init(struct test_struct *t);
void mod2_use(struct test_struct *t);
#define INT_MAGIC 0x42424242
#define FLOAT_MAGIC 42.42
#define LONG_MAGIC 0x1f1f1f1f1f1f1f1fll
EOF
cat > mod1.c <<EOF
#include <stdlib.h>
#include "common_hdr.h"
struct test_struct {
int a;
float b;
long long c;
};
struct test_struct *alloc_test_struct(void) {
return (struct test_struct*)malloc(sizeof(struct test_struct));
}
void mod1_init(struct test_struct *s)
{
s->a = INT_MAGIC;
s->b = FLOAT_MAGIC;
s->c = LONG_MAGIC;
}
EOF
cat > mod2.c <<EOF
#include <assert.h>
#include "common_hdr.h"
struct test_struct {
long long c;
int a;
float b;
};
void mod2_use(struct test_struct *s)
{
assert(s->a == INT_MAGIC);
assert(s->b == FLOAT_MAGIC);
assert(s->c == LONG_MAGIC);
}
EOF
cat > main.c <<EOF
#include <assert.h>
#include "common_hdr.h"
int main() {
struct test_struct *t = alloc_test_struct();
assert(t != 0);
mod1_init(t);
mod2_use(t);
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment