nixpkgs icon indicating copy to clipboard operation
nixpkgs copied to clipboard

"SDL2_image" package does not have access to SDL.h header

Open ClementBaziret opened this issue 1 year ago • 2 comments

Describe the bug

I have a C++ project that i know works because we were multiple persons on it and it was evaluated by our teachers. I switched from Fedora to NixOS very recently and wanted to test some of my projects to sort of assert that I could start future projects safely. This specific project uses the sfml, ncurses and the SDL2 with the SDL2_image library. When I tried to compile it (with g++), it crashed on this line #include <SDL2/SDL_image.h> because :

/nix/store/8md3c1mynawp6nvgxi8nfzfw8zyfgfm0-SDL2_image-2.8.2/include/SDL2/SDL_image.h:32:10: fatal error: SDL.h: No such file or directory
   32 | #include "SDL.h"
      |          ^~~~~~~

I'd like to remind that I am a beginner with nix and nixos so I may be totally wrong and this package could work really well.

Additional context

I tried to compile from a nix-shell that corresponds to this nix flake and which is launched with nix develop :

{
  description = "Simple C++ project flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
  };

  outputs = { self , nixpkgs ,... }:
    let
      system = "x86_64-linux";
    in {
      devShells."${system}".default = let
        pkgs = import nixpkgs {
          inherit system;
        };
      in pkgs.mkShell {
        packages = with pkgs; [
          gnumake
          gcc
          libgcc
          ncurses
          sfml
          SDL2
          SDL2_image
        ];

        shellHook = ''
          echo Custom C++ nix-shell !
        '';
      };
    };
}

This is also the Makefile which is used to compile :


NAME	=	arcade_sdl2.so

CXX		=	g++

INCLUDE_FLAGS	=	-I../../include -I../abstract -I../../include/sprites

CXXFLAGS 	=	-std=c++20 -Wall -Wextra $(INCLUDE_FLAGS) -fPIC

SRC		=	./SDL2.cpp

OBJ		=	$(SRC:%.cpp=%.o)

all: $(NAME)

$(NAME): $(OBJ)
	$(CXX) $(OBJ) -shared -o $(NAME) -lSDL2 -lSDL2_image

debug: $(OBJ)
	$(CXX) $(OBJ) -shared -o $(NAME) -lSDL2 -lSDL2_image -g

clean:
	rm -f $(OBJ)

fclean: clean
	rm -f $(NAME)

re:	fclean $(NAME)

Notify maintainers

@cpages

Metadata

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"

 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.47, NixOS, 24.05 (Uakari), 24.05.20240828.ae2fc9e`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.5`
 - nixpkgs: `/nix/store/s6d8irfyp66lvy9kapbfp2ass36c8lfl-source`

Add a :+1: reaction to issues you find important.

ClementBaziret avatar Aug 30 '24 15:08 ClementBaziret

Usually you need to pass CFLAGS and LDFLAGS that library expects you to. One of the ways to do it is to query pkg-config:

$ pkg-config --libs SDL2_image
-L/nix/store/zpqh4ykgy5fz0pqq7d60y7c97prr4zwd-SDL2-2.30.5/lib -L/nix/store/hksq97w0rmdksazfcxp9ja78r9y9ylwf-SDL2_image-2.8.2/lib -lSDL2_image -Wl,-rpath,/nix/store/zpqh4ykgy5fz0pqq7d60y7c97prr4zwd-SDL2-2.30.5/lib -Wl,--enable-new-dtags -lSDL2

$ pkg-config --cflags SDL2_image
-D_REENTRANT -I/nix/store/jm2fxw52wgnijgkdzwf6pfgrsy7l3sfb-SDL2-2.30.5-dev/include -I/nix/store/jm2fxw52wgnijgkdzwf6pfgrsy7l3sfb-SDL2-2.30.5-dev/include/SDL2 -I/nix/store/hksq97w0rmdksazfcxp9ja78r9y9ylwf-SDL2_image-2.8.2/include/SDL2

Note how --cflags passes ...-SDL2-2.30.5-dev/include/SDL2 include search path to get SDL.h available.

trofi avatar Aug 30 '24 16:08 trofi

Also you don't need gnumake or gcc (and probably not libgcc either) in your shell, those are already provided by stdenv.

eclairevoyant avatar Aug 30 '24 16:08 eclairevoyant

Thanks for your advice @trofi and @eclairevoyant and sorry to have bothered you !

ClementBaziret avatar Aug 30 '24 18:08 ClementBaziret

Ah, no problem! nixpkgs is full of such subtle magic.

trofi avatar Aug 30 '24 19:08 trofi