Author Topic: [SOLVED] Meeting problem of ‘undefined reference to'  (Read 7116 times)

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
[SOLVED] Meeting problem of ‘undefined reference to'
« on: December 13, 2012, 09:10:55 am »
Hello,

I'm just using Code::Blocks 10.05 on Ubuntu 12.04(precise) 64-bit few days.

Firstly, I created a c++ console project called A in /home/test/ccc/, then I created a C static library project called B which includes serveral files: libabc.a, abc.c, abc.h, etc. in  /home/test/abc/

I tired to using the /home/test/abc/libabc.a created by B in A.

But the ide always show the message as title 'undefined reference to ....'

I searched online and operated as most of them suggested as below:
1) right click the project A, and selected 'Build options',
2) click 'Linker settings' tab and click the 'add' button, pick the /home/test/abc/libabc.a from the prject B directionary,
3) click 'Search directories' tab and under in the sub-tab 'complier', there is an 'add' button, click it as well to select the 'abc.h'

That is all. I even add the /home/test/abc to the 'Search directories-Linker' list, the issure is still alive.

Could you help me?
« Last Edit: December 14, 2012, 04:06:15 am by lukenothing »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Meeting problem of ‘undefined reference to'
« Reply #1 on: December 13, 2012, 09:19:57 am »
3) click 'Search directories' tab and under in the sub-tab 'complier', there is an 'add' button, click it as well to select the 'abc.h'
You don't have to select the include file, but the path to the include file, so the compiler is able to find it.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #2 on: December 13, 2012, 09:28:10 am »
Sorry about the time you wasted of, I just address the problem which is that after I add the libabc.a to the 'Link libraries', I also need to add an option to 'Other linker options' under the 'Linker settings', such as 'abc'.

Now the 'undefined reference to ...' is gone, but I get a new problem" g++: error: abc: No such file or directory'

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #3 on: December 13, 2012, 09:37:43 am »
3) click 'Search directories' tab and under in the sub-tab 'complier', there is an 'add' button, click it as well to select the 'abc.h'
You don't have to select the include file, but the path to the include file, so the compiler is able to find it.
Sorry, I make a mistake in description, you right, it'm impossible to add any .h files in that tab, the 'directories' is the only thing it accesses.

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #4 on: December 13, 2012, 10:32:56 am »
It seems that I did not really solve the problem...

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #5 on: December 13, 2012, 11:01:58 am »
I think I'm very close to the root the problem after I switch on the complier logging to 'full command line'

I will be back to show you what is going on after the gym.

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #6 on: December 14, 2012, 03:22:20 am »
Hi everybody, sorry I'm late. I just solve this strange problem. The point is that the project B (libabc.a) is a static library project which compliered by gcc (means c project), and the project A(ccc) is a c++ console application project which is compliered by g++....

after I tried to use the g++ manually compliered the libabc.a, the problem was gone. Everything is OK.

So that I think it's a little BUG of Code::Blocks. For uncertain reason, when you create a static library by Code::Blocks IDE wizard, you will not have the chance to select it as a c or c++ project. However, you can do it through the proces of creating the shared library.

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #7 on: December 14, 2012, 03:29:14 am »
And the question is, where can I change a project language in a static library?

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: Meeting problem of ‘undefined reference to'
« Reply #8 on: December 14, 2012, 04:05:47 am »
HAHA, everything is OK.

I right click the file in static library project B, pick the properties, then select the 'Advanced' tab in properties dialog, in Compiler variable textbox, change the CC to CPP, later on, click the OK button to close this dialog.

Then I rebuild the project B. In the Build log, I see the gcc changed to the g++...

Finally, I switch to the project A, ccc, rebuild it as well.

Now, the only requirement is that I hope there is a in-project global option to switch between CC and CPP, because with the growing of static library project, manually to set each file's properties will be an anxious problem.

Here is the source of solution. http://stackoverflow.com/questions/4695896/codeblocks-gcc-change-project-language-c-and-c
« Last Edit: December 14, 2012, 04:08:31 am by lukenothing »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: [SOLVED] Meeting problem of ‘undefined reference to'
« Reply #9 on: December 15, 2012, 01:46:00 am »
If you wish to do a C++ compiler do NOT name the file with a .c file extension.

Quote
abc.c

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline lukenothing

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: [SOLVED] Meeting problem of ‘undefined reference to'
« Reply #10 on: December 15, 2012, 04:02:44 am »
Thank you,  stahta01, the main.c is created by the CB wizard automatically which I can not control, when I using the wizard to create a static library project.