简体   繁体   中英

How to bundle C/C++ code with C-shell-script?

I have a C shell script that calls two C programs - one after the another with some file handling before, in-between and afterwards.

Now, as such I have three different files - one C shell script and 2 .c file s.

I need to give this script to other users. The problem is that I have to distribute three files - which the users must keep in the same folder and then execute the script.

Is there some better way to do this?

[I know I can make one C code file out of those two... but I will still be left with a shell script and a C code. Actually, the two C codes do entirely different things... so I want them to be separate]

Sounds like you're worried that your users aren't savy enough to figure out how to resolve issues like command not found errors and the like. If absolutely MUST hide "complexity" of a collection of files you could have your script create the other files. In most other circumstances I would suggest that this approach is only going to increase your support workload since semi-experienced users are less likely to know how to troubleshoot the process.

If you choose to rely on the presence of a compiler on the system that you are running on you can store the C code as a collection of cat $STRING >> file.c commands to to create your two C files, which you then compile and use.

If you would want to use pre-compiled programsn instead then the same basic process can be used except instead use xxd to both generate the strings in your script and reverse the conversion process to give you working binaries. Note: Remember to chmod the binary so that it is executable.

use shar command to create self-extracting archive.

or better yet use unzipsfx with AUTORUN option.

This provides users with ONE file, and only ONE command to execute (as opposed to one for untarring and one for execution).

NOTE: The unzip command to run should use "-n" option, that way only the first run would extract the files and the subsequent would skip the extraction.

Use a zip or tar file? And you do realize that .c files aren't executable, you need to compile & link them first?

You can include the c code inside the shell script as a here document :

#!/bin/bash

cat > code.c << EOF
line #1
line #2
...
EOF

# compile
# execute

If you want to get fancy, you can test for the existence of the executable and skip compiling them if they exists.

If you are doing much shell programming, the rest of the Advanced Bash-Scripting Guide is worth looking at as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM