Skip to content

Instantly share code, notes, and snippets.

@gnowzil
Last active June 19, 2020 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnowzil/4bfc0b6c0ec725d0188bded45c885162 to your computer and use it in GitHub Desktop.
Save gnowzil/4bfc0b6c0ec725d0188bded45c885162 to your computer and use it in GitHub Desktop.
Makefile for SNOPT/AMPL from library
#-----------------------------------------------------------------------
# Makefile to create SNOPT AMPL executable from libsnopt7.a
#
# Assumes you have downloaded solvers.tgz and snopt.tgz and
# that you ran "./configurehere; make" in the solvers
# directory to build amplsolver.a
#-----------------------------------------------------------------------
SNOPT_LIB = /path/to/libsnopt7.a
AMPL_SOLVERS_DIR = solvers
SNOPT_AMPL_DIR = snopt
AMPL_INCLUDE = $(AMPL_SOLVERS_DIR)
#-----------------------------------------------------------------------
all: snopt_ampl
clean: clean_ampl
snopt_ampl: $(SNOPT_AMPL_DIR)/snopt.o
$(FC) $(FCFLAGS) -o $@ $(SNOPT_AMPL_DIR)/snopt.o $(AMPL_SOLVERS_DIR)/amplsolver.a $(SNOPT_LIB) -ldl
$(SNOPT_AMPL_DIR)/%.o: $(SNOPT_AMPL_DIR)/%.c
$(CC) $(CFLAGS) -I$(AMPL_INCLUDE) -c $< -o $@
#-----------------------------------------------------------------------
clean_ampl:
$(CLEAN) rm -f $(SNOPT_AMPL_DIR)/snopt.o
#-----------------------------------------------------------------------
.PHONY: clean_ampl
@gnowzil
Copy link
Author

gnowzil commented Jun 19, 2020

We also need to create a file fmain.c with the following in it:

	char **xargv;
	extern void MAIN__(void);
	int main(int argc, char **argv)
	{
		xargv = argv;
		MAIN__();
		return 0;
	}

This file should be placed in the 'solvers' directory and makefile in solvers needs to be updated by adding 'fmain.c' to the list of files built (look for 'a = ...').

@gnowzil
Copy link
Author

gnowzil commented Jun 19, 2020

  1. Download solvers.tgz and snopt.tgz from netlib, uncompress. Should create directories 'solvers' and 'snopt'.

  2. Create a file fmain.c inside the solvers directory with the code below and add it to the list of files built by ASL. (look for "a = ..." inside makefile in the solvers dir). (This is also documented in ASL's instructions in README.f77).

char **xargv;
extern void MAIN__(void);
int main(int argc, char **argv)
{
xargv = argv;
MAIN__();
return 0;
}
  1. In solvers directory, run ./configurehere; make. You should see amplsolver.a now.

  2. Copy makefile_ampl above.

  3. Modify SNOPT_LIB to point to the snopt7 library. If necessary, modify the other locations as well. By default, it assumes that the directories 'solvers' and 'snopt' are on the same level as the file.

  • SNOPT_LIB should point to the libsnopt7.a file
  • AMPL_SOLVERS_DIR should point to the 'solvers' directory
  • SNOPT_AMPL_DIR should point to the 'snopt' asl directory.
  1. Run makefile_ampl with the command make -f makefile_ampl. It should create the executable 'snopt_ampl'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment