Author Topic: How do I link files together in codeblock ?  (Read 19933 times)

Offline fabien

  • Single posting newcomer
  • *
  • Posts: 4
How do I link files together in codeblock ?
« on: March 09, 2010, 06:55:21 pm »
Hi everyone,

I have been trying for five days to find the equivalent of

gcc main.c file1.c file2.c -o TEST

both in codeblocks for linux and on windows.

I had before put :

#include "file1.h"
#include "file1.c"
#include "file2.h"
#include "file2.c"

in main.h (with of course #define "main.h" in main.c)
but I do not think it has the effect I am looking for (since I read it is preprocessing intstruction -just a "copy and paste" statement, and not a linking !)

I know it must be a basic question but could someone help me ?
I am no computer scientist but I desesperatly need to make my algorithm work for my project.

I have read somewhere it is enough that the files be in the same project, that codeblock makes the links. It is not true. It doesn't work !

Sincerely,

fabien

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6666
    • My Best Post
Re: How do I link files together in codeblock ?
« Reply #1 on: March 09, 2010, 07:18:49 pm »
TURN ON Full Compiler Logging!
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F


Then, post what the build log is currently and then below that say what you think is wrong.

Note: The reason for Caps and exclamation point is I have tried to help 5 people know who all failed to turn on "Full Compiler Logging" If you fail I will just give up helping newbies.

Edit 1: Code::Blocks is a two pass system; you will never get exactly what you want. You might wish to use a custom MakeFile; but, since that is hard for a newbie not sure what is best.

Code: [Select]
gcc main.c file1.c file2.c -o TEST

Below is likely to be linking command
Code: [Select]
gcc main.o file1.o file2.0 -o TEST


Tim S.
« Last Edit: March 09, 2010, 07:24:18 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline fabien

  • Single posting newcomer
  • *
  • Posts: 4
Re: How do I link files together in codeblock ?
« Reply #2 on: March 09, 2010, 09:39:10 pm »
Thank you for your help, Tim.
I turned on the full compiler loggin, but it doesn't say it has problem while compling nor does it say what it links : just the black window of the program running in what seems to be an infinite loop. Still, the file I use (nauty) is a package widely used hence I do not think there is a mistake in it.

I am looking at
http://www.opussoftware.com/tutorial/TutMakefile.htm#Dependency%20Lines:

to learn how to make a makefile.

Could you just tell me if I am wrong that #include "blabla.c", etc, are NOT what I should do to link files together ? In fact the only #include I should have is
#include main.h
in main.c

and
#include librairies in main.h?


Offline stahta01

  • Lives here!
  • ****
  • Posts: 6666
    • My Best Post
Re: How do I link files together in codeblock ?
« Reply #3 on: March 09, 2010, 11:01:57 pm »
This is NOT a site to teach programing.

You did NOT post Build Log.
You are trying to include files that are not headers; this is NOT normally done.

Without info, there is no way to help you.
I do not have desire to read web pages to learn what you are supposed to learn.

Goodbye,

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline fabien

  • Single posting newcomer
  • *
  • Posts: 4
Re: How do I link files together in codeblock ?
« Reply #4 on: March 10, 2010, 08:06:57 am »
Hi,

Could you JUST tell me how one usually LINK files in Codeblocks ?

In most manual about programming they spend plenty of time of talking about loops, variables and other things any idiot can understand but they don't mention linking enough ! The manual of codeblock is a example where they describe a lot of things that are details and to not tell you how you link your file !

It would help me a lot.

fabien

Offline fabien

  • Single posting newcomer
  • *
  • Posts: 4
Re: How do I link files together in codeblock ?
« Reply #5 on: March 10, 2010, 08:16:45 am »
I did "Build"->"Build workspace"  and then the build log I obtained was :

-------------- Build: Debug in GraphsForNauty ---------------

Target is up to date.

-------------- Build: Debug in illustration5 ---------------

Target is up to date.

-------------- Build: Debug in EasyNauty2 ---------------

Target is up to date.
Nothing to be done.

And the program runs but never finish.


Then I erased the #include filex.c because as you said they shouldn't be there and I got in the build log :


-------------- Build: Debug in GraphsForNauty ---------------

mingw32-g++.exe  -o bin\Debug\GraphsForNauty.exe obj\Debug\main.o   
obj\Debug\main.o: In function `main':
C:/langageC/GraphsForNauty/main.c:226: undefined reference to `_nauty_check'
C:/langageC/GraphsForNauty/main.c:238: undefined reference to `_nauty'
C:/langageC/GraphsForNauty/main.c:335: undefined reference to `_dispatch_graph'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings
 


Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9508
Re: How do I link files together in codeblock ?
« Reply #6 on: March 10, 2010, 08:28:31 am »
And the program runs but never finish.
Then I erased the #include filex.c because as you said they shouldn't be there and I got in the build log :

Look: The first one is not a Code::Blocks issue - you did something wrong in your application obviously. This forum can't and won't help you because it is out of the scope of Code::Blocks.

The second one is related to basic understanding of programming. Of course you receive linker errors like "undefined references" if you remove parts of the implementation. However, including plain C files instead of just header files in your code is something not very common, but maybe needed within the scope of your work. Again: This forum can't and won't help you because it is out of the scope of Code::Blocks.

Some final hints before I lock the topic as it massively violates our forum rules:
- Grab yourself a book/article about the basics of how a compiler / linker works.
- Try compiling an application of 2..n files at the command line to understand what you have to provide to the compiler / linker.
- Then return back to the (any) IDE and setup the project accordingly concerning include path's for the compiler/linker, flags for the compiler/linker, files etc.
- Instead of including the C file, add the file to your project so it gets compiled / linked into your application.
- When working with more than one source file you will need to setup a project.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ