1

I want to work with Allegro library in clion. I used homebrew and "brew install allegro" command to install allegro. now when I include , and run just a simple code like this:

#include <stdio.h>
#include <allegro5/allegro.h>

int main() {
    printf("Hello World!\n");
    return 0;

}

I get this error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
     (maybe you meant: __Z16_al_mangled_mainv)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [testAllegro] Error 1
make[2]: *** [CMakeFiles/testAllegro.dir/all] Error 2
make[1]: *** [CMakeFiles/testAllegro.dir/rule] Error 2
make: *** [testAllegro] Error 2

I have used the cmake in the correct answer of this question which is:

cmake_minimum_required(VERSION 3.5)
project(testAllegro)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testAllegro ${SOURCE_FILES})

INCLUDE_DIRECTORIES(  /usr/local/Cellar/allegro/5.2.1.1_1/include )
LINK_DIRECTORIES(  /usr/local/Cellar/allegro/5.2.1.1_1/lib )

file(GLOB LIBRARIES "/usr/local/Cellar/allegro/5.2.1.1_1/lib/*.dylib")
message("LIBRARIES = ${LIBRARIES}")

TARGET_LINK_LIBRARIES(testAllegro  ${LIBRARIES})

and still got the same error.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Bytheway
  • 21
  • 4
  • Welcome to Stack Overflow! While you refer to the code in the other SO answer, it is better to include your exact code into your question. It could be that you make some small modification in the code, about which you don't tell us (intentionally or not). Actually, it is a **rule** of the Stack Overflow to include the code into the question which asks about problems in that code. As for the linker error, it should be **descriptive message** about the **error**, like "undefined reference to ". We need to see that error message too. – Tsyvarev May 21 '19 at 20:27
  • Hey, thanks for your response. I tried to edit the post to make it as you said. – Bytheway May 22 '19 at 19:25
  • Please, post also the `CMakeLists.txt` which you use for build your code. – Tsyvarev May 22 '19 at 19:29
  • Done. To be honest I've never changed CMakeLists.txt before. The only thing that I did here was to copy this CMakeLists.txt (as mentioned in the post) and pasted it. – Bytheway May 22 '19 at 20:02
  • In the comments to [that question](https://stackoverflow.com/questions/22459516/undefined-symbol-main-however-i-do-have-a-main-function) it is suggested to declare `int main(void)` instead of `int main()`. – Tsyvarev May 22 '19 at 22:12
  • I tried it and it didn't make any difference. I just learned from [this question](https://stackoverflow.com/questions/49662382/clion-undefined-symbols-for-architecture-x86-64) that this error means compiler can't find the library files. do you think the add_executable line of my cmake is wrong? thanks. – Bytheway May 22 '19 at 23:31
  • No, you "undefined reference" is somehow special: the missed function - `_main` - is normally an alias to the `main`. And you definitely have this function in your source file. So the problem not in the linked libraries, but with the compiler or its settings. You may remove everything related to Alegro from both the source file and from the `CMakeLists.txt`, and the result will be the same (check that!). – Tsyvarev May 22 '19 at 23:41
  • Hey there is this path usr/local/Cellar/allegro/5.2.5.0 in my computer, so I changed all the allegro/5.2.1.1_1 to allegro/5.2.5.0. and got this error: dyld: Symbol not found: __al_mangled_main Referenced from: /usr/local/opt/allegro/lib/liballegro_main.5.2.dylib Expected in: flat namespace in /usr/local/opt/allegro/lib/liballegro_main.5.2.dylib Process finished with exit code 6.am I getting close by doing this? sorry to bother you btw. – Bytheway May 22 '19 at 23:52

0 Answers0