Author Topic: [solved] Compiling C but linker links as C++  (Read 2923 times)

Offline typicalc

  • Multiple posting newcomer
  • *
  • Posts: 18
[solved] Compiling C but linker links as C++
« on: May 14, 2016, 09:53:49 am »
When I link a my C file (console project) with Mingw the build log tab compiles the C files with:
mingw32-gcc.exe -Wall -g  -c mycfile.c -o obj\Debug\mycfile.o
..etc.

but when linking starts it goes like this:
mingw32-g++.exe  -o bin\Debug\myprog.exe obj\Debug\mycfile.o

..and then a lot of "undefined reference to Z8myfuncPC" etc. errors appear.  "It looks like namedecorating is causing problems as "myfunc" is a function in another C file in my project.
The same project & same files compiles and links fine using OpenWatcom

How can I remedy the sitiation?
« Last Edit: May 24, 2016, 04:11:00 pm by typicalc »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Compiling C but linker links as C++
« Reply #1 on: May 14, 2016, 11:50:28 am »
Linking C code with the C++ linker should work just fine.
Can you post your full build log?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline typicalc

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Compiling C but linker links as C++
« Reply #2 on: May 14, 2016, 12:01:33 pm »
I've googled a lot on the subject and many post suggest to use "gcc" instead "g++"  in linking c-files.

The build log is as follows:
mingw32-gcc.exe -Wall -g -masm=intel   -c myfile1.c -o obj\Debug\myfile1.o
mingw32-gcc.exe -Wall -g -masm=intel   -c myfile2.c -o obj\Debug\myfile2.o
..
etc. no warnings.
Then comes the linker part:
mingw32-g++.exe  -o bin\Debug\myprog.exe obj\Debug\myfile1.o obj\Debug\myfile2.o ... (other files)... obj\Debug\myfilen.o   -lgdi32 -luser32 -lkernel32 -lcomctl32
obj\Debug\MICOM.o: In function `Z6somefuncLONG':
myfile1.C:15: undefined reference to `somefuncin_myfile2(int)'
myfile1.C:157: undefined reference to `somefuncin_myfilen(int)'
...
etc. It is just the same - no other errors or warnings

So, how do I tell Codeblocks to use "gcc" instead "g++"  in linking c-files?
 I'd like to try if that fixes my problem.
« Last Edit: May 14, 2016, 12:10:54 pm by typicalc »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Compiling C but linker links as C++
« Reply #3 on: May 14, 2016, 12:59:15 pm »
Post the full log without alterations and using code or quote tags, please.
And use copy paste, not typing from memory.
Also keep in mind that .C files are compiled as C++ file not C, for that you use .c!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline typicalc

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Compiling C but linker links as C++
« Reply #4 on: May 14, 2016, 02:27:33 pm »
Quote
Also keep in mind that .C files are compiled as C++ file not C, for that you use .c!

All my files a C and not CPP. The compiler oftion is also set to "CC".

What do you mean that my files should be c (lowercase) and not C (uppercase). I am working on Windows 7 system.

Offline typicalc

  • Multiple posting newcomer
  • *
  • Posts: 18
Re: Compiling C but linker links as C++
« Reply #5 on: May 14, 2016, 02:36:35 pm »
Success! After renaming my C source files lowercase the problem went away.