NO

Author Topic: C Tutorial, new project needed for every new file?  (Read 7253 times)

Mathias

  • Guest
C Tutorial, new project needed for every new file?
« on: August 22, 2013, 02:19:59 PM »
Hello,

just startet trying to learn C with several tutorials using Pelles C. No my question is, is it really necessary to create for every source file from the tutorial a ne project?

Actual if i try to compile a second source file in my project "Tutorial" i get the error:

POLINK: error: Symbol '_main' is multiply defined

I don't wont compile multiple sourcefiles, i want compile several single sourcefiles without creating a new project for every file.

Is there a way to do that?

Thanks in advance
Mathias


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: C Tutorial, new project needed for every new file?
« Reply #1 on: August 22, 2013, 04:51:27 PM »
PellesC IDE is organized to work for projects.
If you want to compile many single file samples you have two ways:
Use command line - Open a consolle window go the directory where are your files, first tells to os where to find compiler executable with the command povars32 or povars64 (depends if you are developing for 32 or 64 bits) then run CC on the file
Code: [Select]
C:\My_files\MysourcesLocation> povars32
C:\My_files\MysourcesLocation> cc myfile.c
If you want remain in the IDE you can remove the current file from project (left click file in the project pane, from popup menu select delete) then add to project the new file. The boring problem this way is that the executable have always the same name (and overwrites the last).
You can also use makefiles, but I think this is too heavy for beginners.
This procedures are effective on consolle programs, but to compile windows samples you need to add some more switches on 'CC' command line.
« Last Edit: August 22, 2013, 05:01:45 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Mathias

  • Guest
Re: C Tutorial, new project needed for every new file?
« Reply #2 on: August 22, 2013, 05:52:32 PM »
Hello Frankie,

many thanks for your answer. The command line version you described works fine form me in moment.

Regards
Mathias

Offline jcfuller

  • Member
  • *
  • Posts: 36
Re: C Tutorial, new project needed for every new file?
« Reply #3 on: August 22, 2013, 09:38:25 PM »
This is the batch file I use for compiling from RadAsm3.

Make sure to uncomment and set PELPATH

James

Quote
@setlocal
@ECHO OFF
:: *********************************************************************
:: PEL.BAT -> create Windows GUI CON DLL using PellesC
:: *********************************************************************
:: Parameter 1 source file name no extension
:: Parameter 2 should be -m32 -m64
:: Parameter 3 should be CON for console
;;                       GUI for windows
::                       DLL for dynamic link library
::                       OBJ for object file
:: uncomment for multi-threading
:: SET PELMT=-MT
:: *********************************************************************
:: IF[%1]==[] GOTO usage
:: get just file name. RadAsm3 passes complete path with non project files
:: ----------------------------
 SET F=%~nx1
:: ----------------------------
IF [%2] == [] GOTO usage
IF [%3] == []   GOTO usage


IF NOT EXIST "%F%.c" GOTO usage
:: PELPATH is set in RadAsm3 Environment Options
:: or Remove :: if not using RadAsm3 and edit PELPATH
:: SET PELPATH="C:\PellesC7R"


IF /I [%2] == [-m32] (
  CALL %PELPATH%\Bin\povars32.bat

  IF /I [%3] == [con] (
    SET CCFLAGS=-std:C11 -Tx86-coff -Ob1 -fp:precise -W1 -Gd -Ze -Zx -Go -J %PELMT%
    SET FTYPE="Windows 32 bit Console App"
    SET LINKFLAGS=-subsystem:console -machine:x86
    SET OUTFILE="%F%.EXE"
  )

  IF /I [%3] == [gui] (
    SET CCFLAGS=-std:C11 -Tx86-coff -Ob1 -fp:precise -W1 -Gd -Ze -Zx -Go -J %PELMT%
    SET FTYPE="Windows 32 bit gui App"
    SET LINKFLAGS=-subsystem:windows -machine:x86
    SET OUTFILE="%F%.EXE"
  )

  IF /I [%3] == [dll] (
    SET CCFLAGS=-std:C11 -Tx86-coff -Ob0 -fp:precise -W1 -Gd -Go -Ze -Zx -Gn -J %PELMT%
    SET FTYPE="Windows 32 bit dll"
    SET LINKFLAGS=-subsystem:windows -machine:x86 -dll
    SET OUTFILE="%F%.DLL"
  )

  IF /I [%3] == [obj (
    SET CCFLAGS=-std:C11 -Tx86-coff -Ob1 -fp:precise -W1 -Gd -Ze -Zx -Go -J %PELMT%
    SET FTYPE="Windows 32 bit obj"
  )

)

IF /I [%2] == [-m64] (
  CALL %PELPATH%\Bin\povars64.bat
  IF /I [%3] == [con] (
    SET CCFLAGS=-std:C11 -Tamd64-coff -Ob1 -fp:precise -W1 -Gd -Ze -Zx -Go -Gn -J
    SET LINKFLAGS=-subsystem:console -machine:amd64
    SET FTYPE="Windows 64 bit Console App"
    SET OUTFILE="%F%.EXE"
  )

  IF /I [%3] == [gui] (
    SET CCFLAGS=-std:C11 -Tamd64-coff -Ob1 -fp:precise -W1 -Gd -Ze -Zx -Go -J 
    SET FTYPE="Windows 64 bit gui App"
    SET LINKFLAGS=-subsystem:windows -machine:amd64
    SET OUTFILE="%F%.EXE"
  )

  IF /I [%3] == [dll] (
    SET CCFLAGS=-std:C11 -Tamd64-coff -Ob0 -fp:precise -W1 -Gd -Go -Ze -Zx -Gn -J
    SET FTYPE="Windows 64 bit Dll"   
    SET LINKFLAGS=-subsystem:windows -machine:amd64 -dll
    SET OUTFILE="%F%.DLL"
  )


)


:: always use the %F%.rc file in the Res Directory if it exists
:: this should handle both projects and individual files with resources

IF EXIST "res\%F%.rc" (
  ECHO Compiling resources.....
  cd res
  %PELPATH%\Bin\porc.exe "%F%.rc" /i res /fo "%F%.res"
  SET PRES="res\%F%.res"
  cd ..
) ELSE (
  IF EXIST "%F%.rc" (
    ECHO Compiling resources.....
    %PELPATH%\Bin\porc.exe "%F%.rc" /i res /fo "%F%.res"
    SET PRES="%F%.res"
  )
)

ECHO Compiling "%F%.c" to a  %FTYPE%

%PELPATH%\Bin\pocc.exe %CCFLAGS% -D_WIN32_WINNT=0x0501 "%F%.c"
IF DEFINED OUTFILE (
%PELPATH%\Bin\polink.exe %LINKFLAGS% "%F%.obj" %PRES% %4 %5 %6 %7 %8 %9 /out:%OUTFILE%
)
ECHO Finished!
IF EXIST "%F%.obj" del "%F%.obj"
GOTO done


:usage
ECHO **************************************************************
ECHO  Usage:  PEL.BAT MainFile  -m32 or-m64 CON GUI DLL OBJ ExtraFile1 ExtraFile2
ECHO  Note:   ExtraFiles can be .a (import or static libraries) and
ECHO          .o (objectfiles)
ECHO     Use this batch file to easily create your PellesC program
ECHO **************************************************************
:done
endlocal


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: C Tutorial, new project needed for every new file?
« Reply #4 on: August 22, 2013, 10:15:44 PM »
Hello,

just startet trying to learn C with several tutorials using Pelles C. No my question is, is it really necessary to create for every source file from the tutorial a ne project?

Actual if i try to compile a second source file in my project "Tutorial" i get the error:

POLINK: error: Symbol '_main' is multiply defined

I don't wont compile multiple sourcefiles, i want compile several single sourcefiles without creating a new project for every file.

Is there a way to do that?

Thanks in advance
Mathias
NOt quite sure why this such a big deal, but that you need to basically understand that each executable program is considered a "project". And a project can consist of a single source file or out of many, all depending on the size/purpose of that program.
For little learning/tutorial programs, having to use a project for each and every one seems at first a bit overkill but that might level off once you get started with more complex "projects".
The POLINK error you posted above seems to indicate that you tried to compile/build just one of multiple source files that are supposed to work together and hence can not be run all by itself. You can likely "compile" it but not build and run it without additional source files included in that project...

Ralf

Mathias

  • Guest
Re: C Tutorial, new project needed for every new file?
« Reply #5 on: August 23, 2013, 08:56:19 AM »
Good Morning,

ok, my original idea was, to name several projects Tutorial1, Tutorial2, Tutorial3 .... to make a difference in the different chapters of the tutorial i started.
E.g. In Tutorial part 1 would 1 source file the famous "Hello Word". My idea was to store all following "enhanced Hello  World" source files, eg. helloworld.c, helloworld2.c, helloworld3.c under the project Tutorial1. But as described, when i compile/build/run the 2nd "Hello World" the error message will occur.  Using the command line commands i can do it like i want.

Regards
Mathias

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: C Tutorial, new project needed for every new file?
« Reply #6 on: August 23, 2013, 09:00:27 AM »
Mathias,
you can define a 'workspace' holding all projects (see the help on how to use workspaces).

Ralf,
Unfortunately PellesC is missing the 'multitarget' option commonly available in almost all other IDEs that allows to define a project with many executables in the same directory.
So from the point of view of a beginner it seems complicated to define a project for each single sample executable, from the point of view of expert programmers it is annoying to define a workspace with many projects in different directories for a single project that have many distinct executables (as it's normal with code you find around having the main code, like a DLL, and many samples to use it).
The nice point of software developing is that you don't have to 'stay in the line' so imposing a kind of use of PelleC, while you can do in a different way, would only harm the diffusion  ;)
Of course this is my personal point of view, I'm a software developer after all, and don't like to stay 'in the line'  ;D
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: C Tutorial, new project needed for every new file?
« Reply #7 on: August 23, 2013, 01:34:15 PM »
Maybe this helps when testing modified code.
May the source be with you

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: C Tutorial, new project needed for every new file?
« Reply #8 on: August 23, 2013, 08:59:22 PM »
But as described, when i compile/build/run the 2nd "Hello World" the error message will occur.  Using the command line commands i can do it like i want.
But what exactly do you want?

As for the"_main" error of POLINK, you should get the same error using the command line tools, as it simply indicates that in the program you are trying to build/run, the standard function "main()" is missing. That is a basic requirement of each and every C program, and no difference between using the IDE or the command line tools.
I suspect that you are only "compiling" on the command line, not actually "linking" this into an executable program. Compiling in the IDE is likely to succeed as well, the error is explicitly from the linker, the last/next step in creating an executable program.

And the reason WHY you get that error could be as simple as a typo (for example typing "Main" in the source code instead of "main", as C is case-sensitive!)...

Ralf

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: C Tutorial, new project needed for every new file?
« Reply #9 on: August 23, 2013, 09:04:17 PM »
Ralf,
Unfortunately PellesC is missing the 'multitarget' option commonly available in almost all other IDEs that allows to define a project with many executables in the same directory.
So from the point of view of a beginner it seems complicated to define a project for each single sample executable, from the point of view of expert programmers it is annoying to define a workspace with many projects in different directories for a single project that have many distinct executables (as it's normal with code you find around having the main code, like a DLL, and many samples to use it).
The nice point of software developing is that you don't have to 'stay in the line' so imposing a kind of use of PelleC, while you can do in a different way, would only harm the diffusion  ;)
Of course this is my personal point of view, I'm a software developer after all, and don't like to stay 'in the line'  ;D
Might all come down to discipline...  ;)

I am programming in C for roughly 35 years now, with pretty much every C compiler that is/was out there and the "imposed requirement" of Pelle's C in using projects this way bugged me for the better part of the first 15 minutes. And that was years ago...

As I mentioned before, I suspect that our newbie friend is running actually in other issues, as the error that he posted is linker specific, which is automatically invoked by the Pelle's C IDE when you build/run a program/project. He should get the same error on the command line or something is quite right...

Ralf

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: C Tutorial, new project needed for every new file?
« Reply #10 on: August 23, 2013, 10:07:35 PM »
But what exactly do you want?

As for the"_main" error of POLINK, you should get the same error using the command line tools, as it simply indicates that in the program you are trying to build/run, the standard function "main()" is missing. Ralf
Quote
POLINK: error: Symbol '_main' is multiply defined
???
E.g. In Tutorial part 1 would 1 source file the famous "Hello Word". My idea was to store all following "enhanced Hello  World" source files, eg. helloworld.c, helloworld2.c, helloworld3.c under the project Tutorial1. But as described, when i compile/build/run the 2nd "Hello World" the error message will occur. 
Use exclude option in project.
« Last Edit: August 23, 2013, 10:29:49 PM by timovjl »
May the source be with you