Author Topic: C::B/Linux - run console and LD_LIBRARY_PATH  (Read 17105 times)

Offline amarty

  • Multiple posting newcomer
  • *
  • Posts: 41
C::B/Linux - run console and LD_LIBRARY_PATH
« on: September 17, 2008, 12:41:21 am »
 Hello!

I have a workspace with two projects - one is shared library libmy.so, another is exe my, output directory is same for both. I add $(TARGET_OUTPUT_DIR) to library search path for linking executable, and it's linked good. Then I try to run executable and get error: libmy.so not found. I think I need to modify option "Terminal to run console programs" and add to LD_LIBRARY_PATH output directory of my projects, but I don't know how to do this. Anybody can help me?

I wrote next script:

Quote
#!/bin/bash
# cbrun title command args ...
# set up Codeblocks parameter - terminal to launch console program - cbrun $TITLE

dirname()
{
local dir="${1%${1##*/}}"
[ "${dir:=./}" != "/" ] && dir="${dir%?}"
echo "$dir"
}

title=$1
shift
command=$2
dir=`dirname "$command"`
PATH=`echo $dir`:$PATH
LD_LIBRARY_PATH=`echo $dir`:$LD_LIBRARY_PATH
export PATH
export LD_LIBRARY_PATH
xterm -T "$title" -e $@
#xterm -T "$dir" -e $@

place it to /usr/bin and set up the Codeblocks parameter (Settings/Environment) - terminal to launch console program - cbrun $TITLE

it runs, but the same error occurs - error while loading shared libraries: libmy.so: cannot open shared object file: no such file or directory.

I run xterm manualy. go to output dir. run ./my - lib not found
Next, I wrote script:
Quote
#!/bin/bash
export PATH=`echo $PWD`:$PATH
export LD_LIBRARY_PATH=`echo $PWD`:$LD_LIBRARY_PATH
my

It works good. How can I get the same result from Codeblocks environment?
« Last Edit: September 17, 2008, 02:05:39 pm by amarty »

Offline amarty

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #1 on: September 17, 2008, 02:26:04 pm »
 Setting up "Execution working directory" in Project properties/Build targets does not work too.

Offline amarty

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #2 on: September 17, 2008, 03:38:04 pm »
I found plugin EnvVar (in Settings/Environment/Environment variables) and try to set up LD_LIBRARY_PATH as:
Quote
LD_LIBRARY_PATH=$(TARGET_OUTPUT_DIR):$LD_LIBRARY_PATH
but it isn't work too.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #3 on: September 17, 2008, 03:54:08 pm »
I found plugin EnvVar (in Settings/Environment/Environment variables) and try to set up LD_LIBRARY_PATH as:
Quote
LD_LIBRARY_PATH=$(TARGET_OUTPUT_DIR):$LD_LIBRARY_PATH
but it isn't work too.
Accessing macro variables within the envvars plugin is not supported (because it would be hard to distinguish if it's a environment variable *or* an internal one)... But hey - that's a nice idea for a feature to implement. :-)

Ooops - it *is* implemented (completely forgotten I did that :oops:). But it's probably not working because the C::B core modifies this variable, too. In addition the variable might be resolvable at the stage you are trying to access it.
« Last Edit: September 17, 2008, 04:31:17 pm by MortenMacFly »
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 amarty

  • Multiple posting newcomer
  • *
  • Posts: 41
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #4 on: September 17, 2008, 04:31:20 pm »
Accessing macro variables within the envvars plugin is not supported (because it would be hard to distinguish if it's a environment variable *or* an internal one)... But hey - that's a nice idea for a feature to implement. :-)

As sample, sintax of environment variable usage can be changed - $VAR, $(VAR) - C::B var, #VAR, #(VAR) - environment variable. after substitution of C::B var simple change # to $.


It seems that I have found solution of my problem. I have set launch terminal string to [konsole --name "$TITLE" -e] and have set program working directory (Execution working dir) to output directory, where exe and .so located. But how can I use exe and .so in different folders, or change working dir to another path, I don't know.

jsarao

  • Guest
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #5 on: January 06, 2009, 01:07:58 am »
TOOL CHAIN:
Code::Blocks 8.02 Build: Feb 27 2008, 21:58:34 - wx2.8.7 (Linux, unicode)
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
[Did I miss anything?]

PROBLEM:

I have a similar setup, but a slightly different problem. I have a workspace with 2 projects. Project A: creates a shared (dynamic?) library (MyLib.so).  Project B: creates a console application.

I build Project A, with no problem. I see MyLib.so in the output directory.
I try to have Project B link MyLib.so, but I always get the following output in the "Build log":

-------------- Build: Debug in VideoSvrLinux ---------------

Linking console executable: bin/Debug/MyConsoleApp
/usr/bin/ld: cannot find -lMyLib.so
collect2: ld returned 1 exit status

So my console application (Project B) does not even link.

QUESTION: does someone have a very simple example of a Workspace, Project File, Source file that I can use a starting point?

QUESTION: is Code::Blocks "Dynamic library" the same thing as a "Shared Library"?

Project A creates a shared library.
Project B links in that shared library.

Thank in advance.

NOTES:
* tried copying MyLib.so to /usr/local/lib and edited the /etc/ld.so.conf to include that path; no success
* previously Project A was just generating a Static library (libMyLib.a) and I could link against that no problem.






Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #6 on: January 06, 2009, 01:19:15 am »
Add the path where the dynamic (shared) library gets created as linker searchpath for your console app.

jsarao

  • Guest
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #7 on: January 06, 2009, 10:14:16 pm »
Tried that.  Both a relative path and fixed path with no success.


Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #8 on: January 06, 2009, 10:20:20 pm »
I'm not a Unix or Linux expert, but isn't LD_LIBRARY_PATH only used for compiling and linking?  Don't you need to use LD_RUN_PATH when executing the program?

Ringo

siege

  • Guest
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #9 on: July 11, 2009, 09:56:23 pm »
I run in the same problem and since i couldn't find solution on this forum i decided to post one.

The problem is that xterm do not propagate environment variables.
So i use the following script
Quote
#!/bin/bash
# usage: cbrun title args
xterm -T $1 -e bash -c 'export LD_LIBRARY_PATH=$0;shift;$@' $LD_LIBRARY_PATH $@

To use set up Codeblocks parameter (Settings/Environment) - terminal to launch console program:
<path_to_script> $TITLE

I tried to make it work directly from parameter (without script) but it become too complex. Maybe i just miss something.

---------
And about second one. For some reason C::B doesn't add "lib" prefix to dynamic libraries but linker expect it. Just add it by hand (so output of project A become libMyLib.so). (But i think this problem was solved lOOng ago.)
« Last Edit: July 11, 2009, 09:58:02 pm by siege »

Offline Mauricio

  • Single posting newcomer
  • *
  • Posts: 3
Re: C::B/Linux - run console and LD_LIBRARY_PATH
« Reply #10 on: December 11, 2023, 08:01:06 am »
I have this problem now, the fix did not resolve it for me...