Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: MannyMoe on December 18, 2018, 10:21:08 pm

Title: Adding files to project
Post by: MannyMoe on December 18, 2018, 10:21:08 pm
Hi guys:
   I have some programs I want to execute from my Desktop in Linux. They require loading
Jpeg files. If I don't create a project they will Not run from the Desktop. So, the last few days
I have been trying to figure out how to create a project using Code:Blocks. I have been able
to create the project after some time. My problem seems to be adding files to the project.
When I go to Project - Add Files the window shows up displaying the cbp file. Then I go to
the Directory and pick on my C file and click Open at the bottom. I only get a blank screen.
Evidently this is NOT the proper way to do this. If someone has the time, could you show
the correct way to create the Project and how to load the files in order to compile and link
a program. I have been using Code:Blocks for some time, but never using the project feature.
I would always run my programs from the Directory where the file was located. Now I want
to run them from an Icon on the Desktop. Seems simple but is beyond me how to do this!
Thanks for any help you can offer.

I have switch to version 10.5 Code:Blocks, now I am getting 'Can't read timestamp'.
What is up with that!
Title: Re: Adding files to project
Post by: MannyMoe on December 30, 2018, 04:51:04 pm
I have finally gotten my project file completed!
Everything looks great. The file runs perfectly as
long as I am still in codeblocks. When I go to Debug
and try to run the executable, the screen blinks and
is turns black. There is still something I am missing
but I do not know what it is! Has anyone got any ideas
why my file will not run? Thanks for any help you can
offer!
Title: Re: Adding files to project
Post by: BlueHazzard on December 30, 2018, 07:52:31 pm
http://wiki.codeblocks.org/index.php/FAQ-Compiling_(general)#Q:_My_program_runs_in_codeblocks_but_if_i_start_it_from_the_outside_it_only_flashes_and_closes_immediately
Title: Re: Adding files to project
Post by: MannyMoe on December 30, 2018, 08:44:05 pm
BlueHazzard:

   Thanks for the reply. This is not the problem with my project.
My file depends on hitting the ESC key or clicking the Close
button. Is there any setting in Codeblocks that could be causing
my problem? I have tried version 10.05 and version 13.12 and
both do the same thing. I am using ubuntu 16.04 for this project.
Thanks again.
Title: Re: Adding files to project
Post by: BlueHazzard on December 30, 2018, 09:12:53 pm
Quote
My file depends on hitting the ESC key or clicking the Close
Ok, now you have to specify more closely what type of application you are making. What framework you are using and what it is loading/doing. Without this information it is hard to help...

From my mind there could be 2 reasons for this behavior
1) Your application can not find some dynamic library and crashes (try to open it in command line, or use the ldd tool to find any missing library, or start it with gdb from outside codeblocks). Remember that codeblocks adds the path to the linked libraries automatically to ?LD_LIBRARY_PATH? before it runs the application.
2) Your application can not find some input file (you mentioned you are using jpg files in your program) and you do not do any proper error handing on file not found. Remember that if you run a program trough codeblocks the search path for external files begins in the project folder and if you run your application from double click the search path for external files starts from the place where the executable is

Generally speaking your application goes in one error mode you are not handling and you get an unexpected exit. This is bad programming and should be avoided. Luckily you have found the problem and can now fix it.

[edit:] You will be amazed how better codeblocks is with version 17.12. Please poke the maintainer of ubuntu to update the codeblocks version...
Title: Re: Adding files to project
Post by: MannyMoe on December 30, 2018, 10:22:34 pm
BlueHazzard:

  Thanks again for the reply. I believe this is getting way over my head.
I don't think I can follow what you are saying. I believe this is something
I need to stay away from. I am afraid I will really mess things up! This is
my first time trying to make a project file using codeblocks. I have been
going to the directory and running my programs from there. I thought
it would be nice to be able to run them from the Desktop. I need to learn
much more than I know right now! Thanks again for your time. It is nice
to see that you seem to know a great deal. Have a wonderful day my friend!
Title: Re: Adding files to project
Post by: BlueHazzard on December 31, 2018, 10:55:25 am
I do not understand your fear about projects. They are exactly the same as if you build one single file, except that you manage your build a bit.

With all your writing you did not add any information that makes it easier to help you. Instead of whinging about how complicated projects are and how you still have to learn you can simply answer the questions and we will guide you through your problem. It is not rocket science...

To make it even a bit more easier, some checklist you can answer:
1) What library do you use for your user interface? (wxWidgets, GTK, QT...)
1.1) Does this library is linked statically or dynamically?
1.2) How do you installed this library? Was it on the system? Through the packet manager?
2) How do you load the image file?
2.1) What function do you use to load the file
2.2) How do you specify the path to the file you load?
2.3) What do you do if the loading of the file fails?
2.4) Where is the file located? Relative to your executable?
Title: Re: Adding files to project
Post by: MannyMoe on December 31, 2018, 01:36:10 pm
BlueHazzard:

    I am sorry for some of the things I said! I did not mean any disrespect. I just do
not know how to answer some of your questions. Here are some things that might
help you:

I do not use anything fancy. I only use codeblocks, allegro5 and opengl for my programs.
I use only their functions to load my images. All my programs run perfectly from their
directories. You are correct about the problem being the path to the images. When I load
an image, if I include the absolute path to the image and then compile, link, and copy to
the desktop all is perfect. I know this is not the correct way to do things, but it works.
For the first time in my life I tried to use the Debugger. This is what it reported:
warning: GDB: failed to set controlling terminal: operation not permitted

The last thing I did that really made a difference was to set the working directory to the
place where my programs were located.

All my programs are single files. The only reason to get involved with a project was so I
could run them from my desktop.

As far as static or dynamic. I have no idea.

All the libraries and header files are in their correct place and are working perfectly.

The project file (cbp file) is located in a subdirectory of the working directory.

Hope this helps. I will try to stay calm in order to see this thing done!

Thanks and have a great day!
Title: Re: Adding files to project
Post by: BlueHazzard on December 31, 2018, 02:24:45 pm
Quote
    I am sorry for some of the things I said! I did not mean any disrespect
This was not disrespectful to me, but to yourself. You have to have some confidence ;)

Quote
You are correct about the problem being the path to the images.
Great! Now you know how to solve it :)

Quote
GDB: failed to set controlling terminal: operation not permitted
This should be harmless. If you  have other problems during debugging, make sure you enable debug symbols:
Select the "Debug" target from "Build->Select target" and make a rebuild "Build->Rebuild"
Or enable it in the compiler options: Project->build options->Select your project name on the left->Set a tick at the -g flag->hit ok  and rebuild with Build->Rebuild
And if you still have problems please post a full build log and a debugger log from the debugger tab

Quote
Thanks and have a great day!
same to you!
Title: Re: Adding files to project
Post by: MannyMoe on December 31, 2018, 06:37:38 pm
BlueHazzard:
 
   OK! I did what you said and everything is good. No errors of
any kind! I did run into something the other day I might point
out: LD_LIBRARY_PATH = $LD_LIBRARY_PATH: .
Just a period. I tried to find it again but could not. This may tell
the story right there.
When I went to the Debug directory and clicked on the executable,
same thing again: screen blinks and turns black. I believe that PATH
needs to be corrected but I could not find it.
Also, do I need to upgrade my version of codeblocks or do you think
version 13.12 is ok for what I do?

I might add when I did a rebuild, nothing happened. In checking the
messages, it said everything was up to date. Nothing needed to be done.

Are you one of the developers of codeblocks? Just asking.

Hope all is going well on your end!!!!!!
Title: Re: Adding files to project
Post by: BlueHazzard on January 01, 2019, 08:26:58 pm
Quote
Also, do I need to upgrade my version of codeblocks or do you think
version 13.12 is ok for what I do?
13.12 is at least 5(6) years old... updating won't hurt, there where 100s of bugfixes and improvements...
Title: Re: Adding files to project
Post by: MannyMoe on January 01, 2019, 09:00:32 pm
BlueHazzard:

   I still haven't got this thing to work. How do I static link this project?

   Could that be my problem?

   Still grasping for straws on this.

   Have a great new year!
Title: Re: Adding files to project
Post by: BlueHazzard on January 03, 2019, 11:41:31 am
Quote
   I still haven't got this thing to work. How do I static link this project?
Haven't you found out that the paths to your images are the problem?

If still the paths are your problem i cant try to explain to you how paths work with projects:
1) If you program an application and you open a file with fopen("image.jpg"); Your application will search in the same folder as where it is for the file "image.jpg". This is called the "current working directory", cwd in short.
2) If you start this application from codeblocks, codeblocks will set the cwd to the folder where the project file is located. This is done so you can make a release build and a debug build in different folders, but your resources are always at the same place. You can change the cwd in Project->Properties->Execution working directory. Changing this option will not alter the behaviour if you start your application by double clicking on it
3) So if you open a file in your application using relative paths place the resource file in the project directory when starting your application within codeblocks, and put your resource file near to your application if you start it through double click.

To link your library in your application is dependend on your library. This is not specific something about codeblocks
I have googled for 5 sec and found this link:
https://www.allegro.cc/forums/thread/616656
I do not know how you have set up your project and added the libraries, but if you use pkg-config you will have to change the line to following:
Code
pkg-config --libs --static allegro-static-5 allegro_image-static-5 allegro_primitives-static-5

How did you installed allegro? If you have used the package manager you do not have a library problem...
Title: Re: Adding files to project
Post by: MannyMoe on January 03, 2019, 09:22:11 pm
BlueHazzard:

     I have been working everyday for the last (3) weeks and still coming up
with the same thing. Let's review: My project will run perfectly in codeblocks
after I do a build and run. My file will run perfectly when I go to the directory and
double click the file. If I copy the file generated with codeblocks to my Desktop,
the screen will only blink and turn black. If I include the absolute path to my
image file and build and run the file, outside of a project, the file will run perfectly.
This is just a simple file for testing. If only draws a rectangle at the border of the
screen and displays an image in the center of the screen.
      In looking at the .cbp file, it shows adding the libraries (.so files) to the project.
It also shows the .c file and the image file added. I do not know anything else to do.
The fact that it runs by itself adding the absolute path to the image file, proves where
the problem is, but I can't find out why the project file won't run from the Desktop.
Since it doesn't seem to be a library problem, perhaps the static libraries won't work
either. Me thinks I have found a real problem for the books. I am still very calm!!!!
      While I have you here, tell my why that 'main.c' file keeps showing up and how
do I get rid of it? Very annoying to say the least! Have a good day!
Title: Re: Adding files to project
Post by: stahta01 on January 03, 2019, 09:30:56 pm
Try posting a full build log and maybe someone will see a problem to fix!

http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F)

Edit: Attaching your Code::Blocks project file (.cbp) to a post might also help someone help you.

Tim S.
Title: Re: Adding files to project
Post by: MannyMoe on January 04, 2019, 03:33:47 pm
Hi stahta01:

     I tried to copy the build log but codeblocks had the copy greyed out.

     Here is the codeblocks project file:

Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="yyy" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Release">
<Option output="/home/scott/allegro5/Image/show_imagex" prefix_auto="1" extension_auto="1" />
<Option working_dir="/home/scott/allegro/Image/" />
<Option object_output="/home/scott/allegro5/Image/yyy" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add directory="/usr/include/allegro5/Image" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add directory="/usr/include/allegro5/Image" />
</Compiler>
<Linker>
<Add library="/usr/lib/i386-linux-gnu/liballegro.so" />
<Add library="/usr/lib/i386-linux-gnu/liballegro_image.so" />
<Add library="/usr/lib/i386-linux-gnu/liballegro_primitives.so" />
</Linker>
<Unit filename="../rubiks_cube_orange.bmp" />
<Unit filename="../show_image.c">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<envvars />
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>


Hope this helps! Thanks for our time.
Title: Re: Adding files to project
Post by: stahta01 on January 04, 2019, 05:52:43 pm
Use relative paths for your images in both the CB package and in your code.
Set the working path to where your executable is located. I would also put the images in the same folder as the executable or in a sub-folder.
Note: The above directions are valid for an Windows program with your issue; they may or may not be valid for an Linux program.

Tim S.

Title: Re: Adding files to project
Post by: stahta01 on January 04, 2019, 06:02:37 pm
Hi stahta01:

     I tried to copy the build log but codeblocks had the copy greyed out.


You likely tried to copy the "Build Messages" instead try to copy the "build log" next time.

Tim S.
Title: Re: Adding files to project
Post by: MannyMoe on January 04, 2019, 08:51:27 pm
Hi stahta01:

     No, I tried to copy the Build Log. I tried again and it is still greyed out as before.
You mentioned earlier about using GCC from the command line. If you have the time
could you post what that would look like? Maybe their is enough info from the project
file to come up with something. I would like to try it to see what difference, if any, it
would make. Also, do you think there is some PATH that needs to be corrected to solve
my problem? Thanks!
Title: Re: Adding files to project
Post by: stahta01 on January 04, 2019, 09:48:29 pm
Hi stahta01:

     No, I tried to copy the Build Log. I tried again and it is still greyed out as before.
You mentioned earlier about using GCC from the command line. If you have the time
could you post what that would look like? Maybe their is enough info from the project
file to come up with something. I would like to try it to see what difference, if any, it
would make. Also, do you think there is some PATH that needs to be corrected to solve
my problem? Thanks!

I can not help you.
I think the problem is likely in your code; therefore this site rules says it is off topic.
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)
Title: Re: Adding files to project
Post by: BlueHazzard on January 06, 2019, 08:45:48 pm
1) I do not like your project file... The project file should be in the same folder as the source file, or in a sub folder of the project directory, but not like you do:
Code
yourFolder
|- show_image.c
|- rubiks_cube_orange.bmp
|- SOMEFOLDER_I_DO_NO_KNOW
     |- Your project file.cbp

It should look more like this:
Code
yourFolder
|- Your project file.cbp
|- src
|     |- show_image.c
|- res
     |- rubiks_cube_orange.bmp
2) Why do you add the image to the project? Codeblocks can do nothing with the image. Images are not related with your application. (Unless you do not compile it within the exe, but this is done only on windows as far as i know)
3) I simply can not understand where your problem is...
3.1) Do you have compiling problems?
If yes post a build log. If you can not copy and paste it from within codeblocks you can say codeblocks to put it in a log file and post the log file: Settings->Compiler->Build options (you my have to klick on the > arrow until the tab appears)->Save build log to HTML file->Always output the full command line
3.2) Do you have a runtime problem (your application crashes, does the wrong thing)?
Implement proper error handling and error output in your program. Codeblocks is not responsible for your error handling and for this, this forum is the wrong place as statha01 has posted
3.3) If it runs within codeblocks, but not outside codeblocks see 3.2)

I simply do not know how to help you, because i do not understand what your problem is.
You can try to explain it better with as many details as possible. For example like this:
1) Open codeblocks
2) Create a new console project
3) Write the following code:
Code
A minimal code example to reproduce your problem
4) Hit Build->Build an run
5) Outcome: I get a window but no image is displayed
6) Expected behaviour: I get a window with an image
ecc...