Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: lbertolotti on July 22, 2015, 11:27:52 pm

Title: New to Code:.Blocks
Post by: lbertolotti on July 22, 2015, 11:27:52 pm
I was using a different IDE, but decided to check this one out, as it looked easier when linking libraries into c++. So I just copied a test file I was using before, but it didn't work:

Code

#include <stdio.h>

int main()
{

int numero, somma;
somma=0;
printf ("inserisci un numero diverso da 0 come componente della sequenza, 0 per terminare la fase di acquisizione\n");
scanf ("%d", &numero);
while (numero != 0)
{
somma=somma+numero;
printf("inserisci un numero diverso da 0 come componente della sequenza, 0 per terminare la fase di acquisione\n");
scanf("%d", &numero);
}
printf("La somma dei numeri digitati è: %d\n", somma);

FILE *tab;
tab=fopen ("tabela.csv","w");
fprintf(tab, "Hello world");
fclose (tab);
printf("inserisci un carattere\n");
scanf("%d");
}


Build log:
Code
Files (x86)\GnuWin32\lib\libgslcblas.a"
obj\Release\main.o:main.cpp:(.text.startup+0x0): multiple definition of `main'
obj\Release\gsl.o:gsl.cpp:(.text.startup+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 1 warning(s) (0 minute(s), 0 second(s))

I get error: ld returned 1 exit status

Note: I am using Windows 8, SDK version 1.25.0, as for the complier I was using GNU GCC, what do u suggest I use? I was planning on playing around with GSL
Title: Re: New to Code:.Blocks
Post by: stahta01 on July 22, 2015, 11:33:59 pm
Please read and follow the FAQ
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F)

Please use code tags when posting code or build logs!

Tim S.
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 22, 2015, 11:58:24 pm

Please use code tags when posting code or build logs!

Tim S.

Better now?
Title: Re: New to Code:.Blocks
Post by: ouch on July 23, 2015, 12:34:55 am
Given your programs simplicity, I would just use CB's new project wizard to create a new console project and then just copy and paste your code as needed.

That will save you time from having to tell CB what you want, where you want it, and where everything else is...
Title: Re: New to Code:.Blocks
Post by: BlueHazzard on July 23, 2015, 01:33:52 am

Please use code tags when posting code or build logs!

Tim S.

Better now?

formatting is perfect  :D  , but we still need a full rebuild log to know exactly what you are doing
Title: Re: New to Code:.Blocks
Post by: oBFusCATed on July 23, 2015, 01:46:12 am
formatting is perfect  :D  , but we still need a full rebuild log to know exactly what you are doing
No, we don't need the full rebuild log.
The linker has told him exactly what the problem is.
There are two main functions - one in the main.cpp and one in gsl.cpp obivously this is not allowed,
so one of them should be removed either by deleting it in the code or by removing one of the files from the project.
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 02:36:49 am
Let's simplify things a little bit:

c++ code

Code

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    int numero, somma;
somma=0;
printf ("inserisci un numero diverso da 0 come componente della sequenza, 0 per terminare la fase di acquisizione\n");
scanf ("%d", &numero);
while (numero != 0)
{
somma=somma+numero;
printf("inserisci un numero diverso da 0 come componente della sequenza, 0 per terminare la fase di acquisione\n");
scanf("%d", &numero);
}
printf("La somma dei numeri digitati è: %d\n", somma);

    return 0;
}


Build log:

Code


-------------- Clean: Release in Test Project (compiler: GNU GCC Compiler)---------------

Cleaned "Test Project - Release"

-------------- Build: Release in Test Project (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -O2  -c "C:\Users\lucas_000\Documents\Test Project\main.cpp" -o obj\Release\main.o
mingw32-g++.exe  -o "bin\Release\Test Project.exe" obj\Release\main.o  -s 
Output file is bin\Release\Test Project.exe with size 40.50 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

This works, but I get no exe file. Why?

It says the exe file is saved in the bin\Release folder but... when I try to open it I get:
The program can't start because libgcc_s_dw2-1.dll is missing from computer.

How can that be?
Title: Re: New to Code:.Blocks
Post by: BlueHazzard on July 23, 2015, 10:53:25 am
i never got this warning...
but 1 second of using google gave me this: http://stackoverflow.com/questions/4702732/the-program-cant-start-because-libgcc-s-dw2-1-dll-is-missing with the easiest solution:
Quote
Code::Blocks: add '-static' in settings->compiler->Linker settings->Other linker options.

why do you add "-fexceptions" to the compiler settings? Aren't they enabled by default? (Ah, i see you are trying to compile pure c code with c++, but i think if you are using g++ as compiler then exception are enabled by default, also if your code is pure c (i don't know this exactly), i don't think that g++ can differentiate between c and c++ code)
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 03:38:57 pm
Well that worked, but wouldn't it be more correct to change the compiler?
Title: Re: New to Code:.Blocks
Post by: BlueHazzard on July 23, 2015, 05:58:11 pm
Well that worked, but wouldn't it be more correct to change the compiler?
so you want to compile your program with the c compiler?

then you have to select c at the new project wizard....

(or save your file as .c because c::b distinguishes c and c++ files by their extension...)
Title: Re: New to Code:.Blocks
Post by: stahta01 on July 23, 2015, 06:08:54 pm
Well that worked, but wouldn't it be more correct to change the compiler?
so you want to compile your program with the c compiler?

then you have to select c at the new project wizard....

(or save your file as .c because c::b distinguishes c and c++ files by their extension...)

Also, need to change the CB Compiler config to link with the gcc instead of g++ command.

Tim S.
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 07:22:57 pm
Ok, one more question: what is the preferred method of installation of GSL for working with Code::Blocks in Windows 8?

C++

Code
#include <stdio.h>
#include <cstdlib>
#include <gsl/gsl_sf_bessel.h>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    /*Summing a bunch of numbers*/
    int numero, somma;
somma=0;
printf ("Insert a number other than 0 in the sequence, otherwise 0 to end the insertion\n");
scanf ("%d", &numero);
while (numero != 0)
{
somma=somma+numero;
printf("Insert a number other than 0 in the sequence, otherwise 0 to end the insertion\n");
scanf("%d", &numero);
}
printf("The sum equals: %d\n", somma);

/*GSL Bessel function*/

    double x = 5.0;
    double y = gsl_sf_bessel_J0(x);
    printf("J0(%g) = %.18e\n", x, y);

    /*Writing to a text file*/

    FILE *tab1;
tab1=fopen ("somma.txt","w");

fprintf(tab1, "%d", somma);
fclose (tab1);

/*Writing to a csv file*/

ofstream tab2("tabela.csv");

double A; int t;
    tab2 << "Col 1;Col 2" << endl;
    for (t = 0; t <= 10; t++) {

      A = t+somma+y;

      cout << "t = " << t << "\t\tA = " << A << endl;
      tab2 << t << ";" << A << endl;
    }
    tab2.close();

    /*End*/
    system("PAUSE");
    return 0;
}


Build log:

Code

-------------- Clean: Release in Test Project (compiler: GNU GCC Compiler)---------------

Cleaned "Test Project - Release"

-------------- Build: Release in Test Project (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -O2 -ansi -I"C:\Program Files (x86)\GnuWin32\lib" -c "C:\Users\lucas_000\Documents\Test Project\main.cpp" -o obj\Release\main.o
C:\Users\lucas_000\Documents\Test Project\main.cpp:3:31: fatal error: gsl/gsl_sf_bessel.h: No such file or directory
 #include <gsl/gsl_sf_bessel.h>
                               ^
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 
Title: Re: New to Code:.Blocks
Post by: oBFusCATed on July 23, 2015, 08:23:55 pm
Try reading the FAQ on the wiki and especially this page: http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_I_would_like_to_compile_a_project_using_some_non-standard_libraries._How_can_I_indicate_to_CodeBlocks_that_these_libraries_and_include_files_exist.3F

Also read the manual of your compiler and understand how it handles #includes.
And lastly try to understand what is the difference between "" and <> in the include directive.
Title: Re: New to Code:.Blocks
Post by: BlueHazzard on July 23, 2015, 08:39:42 pm
And always post the full build log if you want some help...

greetings
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 08:49:13 pm
And always post the full build log if you want some help...

greetings

Done.
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 08:53:59 pm
Well, I solved it by using this:

http://stackoverflow.com/questions/14202598/installing-gsl-on-windows-xp-32bit-for-use-with-codeblocks

Only linking the libraries was not working.
Title: Re: New to Code:.Blocks
Post by: BlueHazzard on July 23, 2015, 09:07:23 pm
Only linking the libraries was not working.

what does this mean? Do you need again help or is it working now?

(if you need help: we need again a full rebuild  log of the error)

greetings
Title: Re: New to Code:.Blocks
Post by: lbertolotti on July 23, 2015, 09:32:52 pm
I solved it

means it's working now.

Build log:

Code

-------------- Clean: Release in Test Project (compiler: GNU GCC Compiler)---------------

Cleaned "Test Project - Release"

-------------- Build: Release in Test Project (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -O2 -I"..\..\..\..\Program Files (x86)\GnuWin32\include" -c "C:\Users\lucas_000\Documents\Test Project\main.cpp" -o obj\Release\main.o
mingw32-g++.exe  -o "bin\Release\Test Project.exe" obj\Release\main.o  -s -static  "..\..\..\..\Program Files (x86)\GnuWin32\lib\libgsl.a" "..\..\..\..\Program Files (x86)\GnuWin32\lib\libgsl.dll.a" "..\..\..\..\Program Files (x86)\GnuWin32\lib\libgslcblas.a"
Output file is bin\Release\Test Project.exe with size 669.50 KB
Process terminated with status 0 (0 minute(s), 4 second(s))
0 error(s), 0 warning(s) (0 minute(s), 4 second(s))