Home > front end >  C language: main .obj file (and executable) is nearly 400MB in size
C language: main .obj file (and executable) is nearly 400MB in size

Time:05-06

Dependencies:

  • GLFW 3.3.7
  • GLAD 4.0 core
  • CGLM 0.8.5
  • stb_image 2.2.7

Compiler: GCC 11.2.0 (MinGW-W64)
CMake 3.23.1
GNU Make 4.3

Problem

The .obj file for main.c (and executable) is nearly 400MB in size
Generated .obj files: https://i.stack.imgur.com/Dgn51.png

There must something wrong with the main.c file and/or the way I include my dependency headers, but I can't figure it out.

Source

The full source can be found at this github repo https://github.com/kvbc/maincraft

main.c

#include "mc.h"

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <stdio.h>
#include <stdlib.h>

mc.h

#ifndef MC_H
#define MC_H

#include <glad/glad.h>
#include <cglm/cglm.h>
#include <stb_image.h>

#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>

#include "config.h"

Building

I'm using CMake and Make to build GLFW and CGLM as static libraries and my program into an executable.
As far as I'm aware there's no problem with building the first two, but here is how I do it

cmake -S lib/glfw-3.3.7 -B build/glfw -G "MinGW Makefiles" -DBUILD_SHARED_LIBS=OFF -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF -DGLFW_BUILD_DOCS=OFF
make -C build/glfw
:: CGLM
cmake -S lib/cglm-0.8.5 -B build/cglm -G "MinGW Makefiles" -DCGLM_SHARED=OFF -DCGLM_STATIC=ON -DCGLM_USE_C99=OFF -DCGLM_USE_TEST=OFF
make -C build/cglm

Now, as for building my code (CMakeLists.txt)

cmake_minimum_required(VERSION 3.8.2)

project(maincraft)

set(CMAKE_C_FLAGS "-O3 -s -DNDEBUG -Wall -Wextra")

include_directories(lib/glfw-3.3.7/include)
include_directories(lib/cglm-0.8.5/include)
include_directories(lib/glad-4.0-core/include)
include_directories(lib/stb_image-2.2.7/include)

link_directories(build/glfw/src)
link_directories(build/cglm)

add_executable(${PROJECT_NAME}
    lib/stb_image-2.2.7/src/stb_image.c
    lib/glad-4.0-core/src/glad.c
    src/file.c
    src/main.c
    src/program.c
    src/camera.c
    src/world.c
    src/tex.c
    src/math.c
    src/text.c
)

target_link_libraries(${PROJECT_NAME} glfw3)
target_link_libraries(${PROJECT_NAME} cglm)

The stb_image.c file is just a define with an include

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

CodePudding user response:

In the main.c file you have the global variable G, which is a structure containing many other structures.

Among other things the G structure have a member named world which is a mc_World structure.

The mc_World structure have an array of 10000000 elements, each element being a mc_Block structure.

The mc_Block structure is at least 32 bytes large.

The mc_World array block will take up at least 32 * 10000000 bytes, i.e. just a little under 320 MiB!

As an array (indirectly) in the global scope it will exist in the object file, and in the executable file.

Perhaps you should consider dynamic allocation for this array? That will also make it easier to have a world of different sizes.

  •  Tags:  
  • c
  • Related