Choosing Between Multiple Executables with Same Name in Linux

Choosing between multiple executables with same name in Linux

Executables are found in PATH order. You need to prepend ${HOME}/usr/bin to your path, like so:

export PATH="${HOME}/usr/bin:$PATH"

multiple binaries with same name in ubuntu/linux

If you are sure you'll never use the original play command, you could just remove the binary. But in general, this isn't a good idea, since some system component you don't think of might need it, and the next update will probably restore it.

The best thing to do is to prepend the directory of your play command to the PATH, for example, using PATH=/opt/framework/bin:$PATH in your .profile (assuming your play command installs to /opt/framework/bin/play), or the script that starts your web server, or wherever you need your play command.

Remember that does not make your play command global. A common mistake is to add the path in their .profile file, then call the program from crontab - crontab scripts will not execute .profile or .bashrc.

What order does bash use to decide whether to use an alias, function, built-in, or executable?

What order does bash use to decide whether to use an alias, function, or executable?

Alias is not "executed". If aliases are enabled and when an alias matches then the word is replaced. Always.

Then, function first, executable second, if ordering these two.

Aliases are only expanded in interactive shells (not in scripts)

In bash, not really, just interactive shells have aliases enabled by default. You can enable aliases in non-interactive shells and disable in interactive shell - it's a separate property.

If a user-defined function has the same name as an executable, will bash run the function or the executable?

Function.

If a user-defined function has the same name as a bash built-in, will bash run the function or the built-in?

Function.

what is the order of precedence for what bash will run if there are two (or more) things with the same identifier?

The behavior is documented in bash documentation command search and execution. The behavior is standardized in POSIX Shell command language, section Command search and execution.

How to execute multiple executables with a pattern in linux

You can use a short script to accomplish this:

#!/bin/bash
for f in *Test
do
"./$f"
done

Or, even as a one-liner:

for f in *Test; do "./$f"; done

What should I do if two libraries provide a function with the same name generating a conflict?

  • If you control one or both: edit one to change the name and recompile Or equivalently see Ben and unknown's answers which will work without access to the source code.
  • If you don't control either of them you can wrap one of them up. That is compile another (statically linked!) library that does nothing except re-export all the symbols of the original except the offending one, which is reached through a wrapper with an alternate name. What a hassle.
  • Added later: Since qeek says he's talking about dynamic libraries, the solutions suggested by Ferruccio and mouviciel are probably best. (I seem to live in long ago days when static linkage was the default. It colors my thinking.)

Apropos the comments: By "export" I mean to make visible to modules linking to the library---equivalent to the extern keyword at file scope. How this is controlled is OS and linker dependent. And it is something I always have to look up.

in which order will ubuntu search bin-folders for executables?

The PATH shell variable contains a colon separated list of paths to look for executables in. The list is processed left to right, the shell executes the first executable binary it finds (make sure to chmod +x the binary you are providing). If you want an easier printout you can use: echo $PATH | tr ":" "\n". Also keep in mind that a program might have been started with a different PATH than your shell and that users can customize their PATH variable. Systemwide PATH settings can usually be found in /etc/profile or /etc/profile.d/. You can use which file to display the full path expansion of file.

What happens if two headers share the same name when compiling with CMake and c++?

The preprocessor searches for include files in this order:

Quoted form:

  • In the same directory as the file that contains the #include statement.
  • In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.
  • Along the path that's specified by each /I compiler option.
  • Along the paths that are specified by the INCLUDE environment variable.

Angle-bracket form :

  • Along the path that's specified by each /I compiler option.
  • When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.

Adding multiple executables in CMake

My suggestion is to tackle this in two phases:

  1. Build a library from the .cpp and .h files, using add_library
  2. Iterate through all your .cxx files and create an executable from each, using add_executable and foreach

Build the library

This could be something as simple as

file( GLOB LIB_SOURCES lib/*.cpp )
file( GLOB LIB_HEADERS lib/*.h )
add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )

Build all the executables

Simply loop over all the .cpp files and create separate executables.

# If necessary, use the RELATIVE flag, otherwise each source file may be listed 
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
# file( GLOB APP_SOURCES RELATIVE app/*.cxx )
file( GLOB APP_SOURCES app/*.cxx )
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} YourLib )
endforeach( testsourcefile ${APP_SOURCES} )

Some warnings:

  • file( GLOB ) is usually not recommended, because CMake will not automatically rebuild if a new file is added. I used it here, because I do not know your sourcefiles.
  • In some situations, source-files may be found with a full pathname. If necessary, use the RELATIVE flag for file(GLOB ...).
  • Manually setting the source-files requires a change to CMakeLists.txt, which triggers a rebuild. See this question for the (dis-)advantages of globbing.
  • I generated the testname using a string( REPLACE ... ). I could have used get_filename_component with the NAME_WE flag.

Concerning "general" CMake info, I advise you to read some of the broad "CMake Overview" questions already asked here on stackoverflow. E.g.:

  • https://stackoverflow.com/questions/2186110/cmake-tutorial
  • What are the dusty corners a newcomer to CMake will want to know?


Related Topics



Leave a reply



Submit