Author Topic: Linking problem with CLANG & Code::Blocks, linking works with GCC & Code::Blocks  (Read 2020 times)

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Hi, I'm developing this project with Code::Blocks. (check out a gameplay video or this other discussion about it if you want more information. Or the project's Facebook page).



I started programming it with pure modular C, Tilengine for sprites & pixel art etc, SDL2 for mouse control, SDL2_mixer for audio (mp3 music and sound FX), SDL2_ttf for drawing text (including some Japanese characters), and SDL2 image (I don't remember why this one, but I used it somehow or somewhere).

So my general linker options look like this with GCC:
Code
-lm -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image

And a link to Tilengine.lib library:



Please notice this is for the topmost project options "Morgana". Below i have two target builds, one for GCC and one for CLANG. This one is for GCC:



and the linking options vary a little from "Morgana" general linker options:

Code
-lws2_32 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image

I rebooted the development of the project at some point and I'm migrating it to C++ OOP with Code::Blocks and GCC (and the same libraries mentioned). I plan to open source the project some day, and to do a Kickstarter for it, when I have a *MUCH* mature version and could dedicate all the time that this project needs (I have two little daughters).

Meanwhile I have a question. I use Code::Blocks and GCC for compiling without problems, but I want to use CLANG for its static analyzer and to check for more errors, but I have a linking problem. Besides I have no clue on how to trigger the CLANG analyzer. I used CppCheck integrated with Code::Blocks with no problems. I even change the Mingw used for one that uses posix threads for some thread I needed somewhere.

Well, my CLANG linking options are blank, so it uses the ones from "Morgana":



The compilation with GCC is successful:



But it fails with CLANG, since it cannot find m.lib



(cannot find m.lib in spanish)
Code
LINK : fatal error LNK1181: no se puede abrir el archivo de entrada 'm.lib'

But if I remove -lm option then it doesn't compile with GCC. I also used the option "Use target options only" for CLANG build and blank linker options with Tilengine.lib linking at the side.

But then it says there is no entry point (no main). I don't know how to link SDL2 and the other libraries to CLANG. I think I installed them on top of LLVM CLANG directories as I did with MingW, but it doesn't work.

Code
LINK : fatal error LNK1561: se debe definir el punto de entrada

Then I tried to add this options to CLANG linker:

Code
-lm -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image

But then it says that cannot find SDL2main.lib linking error.  ???

I don't understand why, where is that file? When you download SDL2 comes with no .lib files, just .a files and other stuff.

Sorry for the noobness  :-[ :-[ :-[.  I would like to fix the linking with CLANG as it worked with GCC, please can you help me or point me to some useful information?

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Update. Now I tried adding this libraries:

Code
C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\lib\libSDL2main.a C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\lib\libSDL2_mixer.a C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\lib\libSDL2_ttf.a C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\lib\libSDL2_image.a W:\Morgana\Tilengine.lib



It detects the libraries, but now it says it has NO entry point ("se debe definir el punto de entrada" is "you must define an entry point" (main) in spanish):

Code
LINK : fatal error LNK1561: se debe definir el punto de entrada
clang++: error: linker command failed with exit code 1561 (use -v to see invocation)
Process terminated with status 1561 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3027
The best  would be to post full rebuild logs to see where the problem is:
(obligatory link to faq, but the wiki is down ;( ) https://web.archive.org/web/20200123060135/http://wiki.codeblocks.org/index.php/FAQ-Compiling_(general)#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

About the main entry point not finding:
Make sure that your cpp file where the main is located is enabled to build for the clang target:
Project->Properties->Build targets->select the clang target on the left->Make sure build target files in the right bottom is set correctly.

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
TEST: Hi, I'm trying to post an answer but it doesn't work.

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Update 2.

Ok, using some help I added:

Code
-Xlinker /subsystem:windows



And now says machine type of library 'x64' is in conflict with the machine type of the destination 'x86'.

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
OMG OMG OMG I fixed it!

add this to main.cpp:
Code
#undef main
int main(int argc, char *argv[]){ // Do not remove arguments or it will show a SDL_main error.

and linking options go like this:

Code
-lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_ttf -lSDL2_image -Xlinker /subsystem:console

And you must download SDL, SDL ttf, SDL image, etc... for developers Visual Studio version, and extract the .lib to the chocolatey installation folder where MingW64 is located.

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Ah! delete the .a links, it just looks like this:


Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Ah, don't forget to tick this:


Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Now how I do use the famous CLANG static code analyzer?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3027
So the linking problem is solved?

For the next time, please do not post images, but text for the build log. Images use a lot space, are not really readable and searchable. Text logs are more easy to read, need a lot less space and can be searched....


Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
Quote
So the linking problem is solved?

For the next time, please do not post images, but text for the build log. Images use a lot space, are not really readable and searchable. Text logs are more easy to read, need a lot less space and can be searched....

Yes, it's solved :) it works doing what I say. If someone else have the same problem and don't understand please post and I'll help. (English not my mother tongue).

Ok about the images, altough I had some error in the forums about having >2000 characters when using logs inside
Code
[code] tags

Offline jalcazo

  • Multiple posting newcomer
  • *
  • Posts: 23
About using the static code analyzer I tried using scan-build with no luck. Check out this: https://clang-analyzer.llvm.org/scan-build.html

And this other thread when trying to use the alternative clang-tidy: https://forums.codeblocks.org/index.php/topic,22179.new.html#new