Author Topic: !!HELP!!---a simple project which works well under CodeLite, but Code::Blocks  (Read 14367 times)

Offline ichaochao

  • Single posting newcomer
  • *
  • Posts: 4
First, thank you for the GREAT software!
Second, i'm sorry about my poor English!

===================
--OS: WinXP SP3
--IDE: Code::Blocks svn 5911 + MinGW-3.4.5
         CodeLite v2.0.3365 + MinGW-4.4.0
===================

My problem is that I copy a simple C project to C::B and it occurs some errors(see below), but the project works well under CodeLite v2.0 and it's compiled by gcc-3.4.x without any error under RedHat, please tell me what the reason is or where i did wrong!!Thanks!!

The full build log is:

-------------- Build: Debug in binarytree ---------------

mingw32-gcc.exe -Wall  -g     -c "d:\我的文档\CodeBlocks Project\binarytree\main.c" -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\binarytree.exe obj\Debug\main.o    
obj\Debug\main.o: In function `main':
d:/我的文档/CodeBlocks Project/binarytree/main.c:13: undefined reference to `_init'
d:/我的文档/CodeBlocks Project/binarytree/main.c:14: undefined reference to `_pre_order'
d:/我的文档/CodeBlocks Project/binarytree/main.c:16: undefined reference to `_in_order'
d:/我的文档/CodeBlocks Project/binarytree/main.c:18: undefined reference to `_post_order'
d:/我的文档/CodeBlocks Project/binarytree/main.c:20: undefined reference to `_depth'
d:/我的文档/CodeBlocks Project/binarytree/main.c:20: undefined reference to `_count'
d:/我的文档/CodeBlocks Project/binarytree/main.c:21: undefined reference to `_destroy'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)
7 errors, 0 warnings
 
===================

And the project is:

binarytree.h:
Code
#ifndef BINARYTREE_H
#define BINARYTREE_H

typedef struct node *link;
struct node {
     unsigned char item;
     link l, r;
};

link init(unsigned char VLR[], unsigned char LVR[], int n);
void pre_order(link t, void (*visit)(link));
void in_order(link t, void (*visit)(link));
void post_order(link t, void (*visit)(link));
int count(link t);
int depth(link t);
void destroy(link t);

#endif // BINARYTREE_H

binarytree.c:
Code
#include <stdlib.h>
#include "binarytree.h"

static link make_node(unsigned char item)
{
link p = malloc(sizeof *p);
p->item = item;
p->l = p->r = NULL;
return p;
}

static void free_node(link p)
{
free(p);
}

link init(unsigned char VLR[], unsigned char LVR[], int n)
{
link t;
int k;
if (n <= 0)
return NULL;
for (k = 0; VLR[0] != LVR[k]; k++);
t = make_node(VLR[0]);
t->l = init(VLR+1, LVR, k);
t->r = init(VLR+1+k, LVR+1+k, n-k-1);
return t;
}

void pre_order(link t, void (*visit)(link))
{
if (!t)
return;
visit(t);
pre_order(t->l, visit);
pre_order(t->r, visit);
}

void in_order(link t, void (*visit)(link))
{
if (!t)
return;
in_order(t->l, visit);
visit(t);
in_order(t->r, visit);
}

void post_order(link t, void (*visit)(link))
{
if (!t)
return;
post_order(t->l, visit);
post_order(t->r, visit);
visit(t);
}

int count(link t)
{
if (!t)
return 0;
return 1 + count(t->l) + count(t->r);
}

int depth(link t)
{
int dl, dr;
if (!t)
return 0;
dl = depth(t->l);
dr = depth(t->r);
return 1 + (dl > dr ? dl : dr);
}

void destroy(link t)
{
post_order(t, free_node);
}


the main.c
Code
#include <stdio.h>
#include "binarytree.h"

void print_item(link p)
{
printf("%d", p->item);
}

int main()
{
unsigned char pre_seq[] = { 4, 2, 1, 3, 6, 5, 7 };
unsigned char in_seq[] = { 1, 2, 3, 4, 5, 6, 7 };
link root = init(pre_seq, in_seq, 7);
pre_order(root, print_item);
putchar('\n');
in_order(root, print_item);
putchar('\n');
post_order(root, print_item);
putchar('\n');
printf("count=%d depth=%d\n", count(root), depth(root));
destroy(root);
return 0;
}

in additon:
My C::B can build and run others simple C projects success!!
And I change the C::B's toolchain into the CodeLite's MinGW-4.4.0, the project can NOT be compiled success!!
« Last Edit: December 06, 2009, 09:32:24 am by ichaochao »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Is binarytree.c part of the project ?
I don't see where it is compiled.
I also don't see a library that may contain it being linked.

Make sure that both c-files and the header-file are project members.
To add existing files, just right-click your project in the manager-pane and chose "Add files..." .

Offline ichaochao

  • Single posting newcomer
  • *
  • Posts: 4
Thank you jens!!!

Is binarytree.c part of the project ?
YES!

Make sure that both c-files and the header-file are project members.
To add existing files, just right-click your project in the manager-pane and chose "Add files..." .
I'm sure i have done what you said!

[attachment deleted by admin]
« Last Edit: December 06, 2009, 10:22:45 am by ichaochao »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
What does the build log show if you do a rebuild, not just a build, is "binarytree.c" compiled in this case ?

Can you attach the project-file (*.cbp) ?
Just copy it and change the file ending to txt, so you are allowed to attach it here.

Offline ichaochao

  • Single posting newcomer
  • *
  • Posts: 4
Thank you jens again!!

What does the build log show if you do a rebuild, not just a build, is "binarytree.c" compiled in this case ?
It is as same as what i said above!
***undefined reference to***

The binarytree.cbp
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="binarytree" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin\Debug\binarytree" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug\" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin\Release\binarytree" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release\" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="binarytree.c">
<Option compilerVar="CC" />
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="binarytree.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
« Last Edit: December 06, 2009, 10:36:08 am by ichaochao »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Both boinarytree-files do not belong to any build-target.

The easiest way is to remove them from project and the re-add them.
Make sure that the targets they should belong to are checked in the following dialog (in your case Debug and Release).

Alternatively, you can right-click the files, chose properties, go to the build tab and check the targets they should belong to.

Or right-click the projects properties, go to "Build targets" and make sure the appropriate files are checked in the "Build target files" list

Offline ichaochao

  • Single posting newcomer
  • *
  • Posts: 4
Oh, yes!!
jens, thank you very, very, very much!!
it works!!
« Last Edit: December 06, 2009, 11:00:17 am by ichaochao »