Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX: clang / ld says it can't find a static library even though it exists and all parameters are correct

Tags:

c++

linker

clang

ld

I know it's going to be a silly mistake, and I'm aware that there are similar posts however none have helped so far, so I'm resorting to a new post.

I'm trying to compile the simplest GLFW hello world example.

In my project directory (I'm using vscode) there is a main.cpp file, and the glfw lib file I downloaded from glfw.org which I renamed to libglfw3.a (It's precompiled) I am using a MacBook Air M1.

I'm trying to compile it with this command:

clang -I /opt/homebrew/Cellar/glfw/3.3.7/include -L /Users//projects/ -l libglfw3.a main.cpp -o helloglfw

The output I get is:

ld: library not found for -llibglfw3.a clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm clearly dumb because for the life of me I can't see what I'm doing wrong...

Things I've tried:

  1. linking to the homebrew version of glfw which contains a .dylib of glfw
  2. tried linking libglfw3 (without the .a extension)
  3. compiling to an object file and then linking with ld separately
  4. using g++
  5. Not using the -L flag and just pasting the full path into -l
  6. Praying and restarting the pc just in case.
  7. Waiting for it to fix itself

Some assistance would be appreciated.

like image 903
user2268909 Avatar asked Oct 18 '25 12:10

user2268909


1 Answers

OK SO! I fixed it. Apparently it works if there is NO SPACE between the -L and -l parameters.

so the command looks like this:

clang <source files> -I<Include Paths> -L<pathtolibs>-l<libs>

No clue why, but whatever I'm done...

like image 153
user2268909 Avatar answered Oct 21 '25 02:10

user2268909