Can't compile 'C' code in nix-shell or in flake because it does not recognize the library HELP!

Hello! I’ve been trying to get this library (libcs50) to work in a nix-shell and or flake, problem is the shell doesn’t recognize the library every time I try to compile it with gcc, The result is 2 months of torture ha ha!

(I am at my breaking point hehehehehe)

I’ve tried nix-ld to link the files and it doesn’t work I tried making my own simple shell.nix in order to compile it and it doesn’t work. I eventually resorted to getting a flake development environment, guess what? It also doesn’t work! What is even more odd when I first got into NixOs it worked perfectly well, and then something happened to where it just stopped. The error message that it gives it usually to this effect:

/nix/store/2p1sq1nr09xr3xb1a9lrjgdanvk1aakb-binutils-2.42/bin/ld:
/tmp/nix-shell.m1ou1c/cctXhvMD.o: in function `main': debug.c: (.text.startup+0xd): 
undefined reference to `get_string`
/nix/store/2p1sq1nr09xr3xb1a9lrjgdanvk1aakb-binutils-2.42/bin/ld: 
debug.c: (.text.startup+0x20): undefined reference to `get_string$

And here’s my flake:

{
  description = "A Nix-flake-based C/C++ development environment";

  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz";

  outputs = { self, nixpkgs }:
    let
      supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
      forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
        pkgs = import nixpkgs { inherit system; };
      });
    in
    {
      devShells = forEachSupportedSystem ({ pkgs }: {
        default = pkgs.mkShell.override
          {
            # Override stdenv in order to change compiler:
            # stdenv = pkgs.clangStdenv;
          }
          {
            packages = with pkgs; [
              clang-tools
              cmake
              codespell
              conan
              cppcheck
              doxygen
              gtest
              lcov
              vcpkg
              vcpkg-tool
            ] ++ (if system == "aarch64-darwin" then [ ] else [ gdb ]);
          };
      });
    }; 
}

I hope you guys can help me out on this because it’s been miserable trying to get this to work! Thanks in advance.

What library is supposed to provide get_string?

libcs50 probably?​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

cs50.h within the c include library, get_string provides a string output then right after reading an input you provided.

#include <stdio.h>
#include <cs50.h>

int main() {
  string name = get_string("what is your name?");

  //After that we can print the input
  printf("hello %s! Nice to meet you",name);

  return 0;
}

The expected Result would be:

  • What is your name?
    input
  • hello input! Nice to meet you

But it doesn’t do that and I am getting frustrated just thinking about it!

Well two things:

  • You’re missing libcs50 in the list of packages in the shell, as I mentioned above
  • You need to build the program with cc -lcs50 whatever.c
1 Like

I’m going to blow my head off with a fucking buckshot.

Two months…