Josep Borrell, also a former President of the European Parliament, disagreed with the strategy.
The comments were came during a visit Mr Borrell made to the US at the end of June.
Mr Trump's pledge to build a wall between the US and Mexico was one of his best-known election promises.
Mr Borrell recounted his conversation with the US president at a lunch event in Madrid this week, Spain's foreign ministry confirmed to the BBC.
"The border with the Sahara cannot be bigger than our border with Mexico," Mr Borrell quoted Mr Trump as saying.
The US-Mexico border is 1,954 miles (3,145 km) long. The Sahara desert stretches for 3,000 miles.
Spain has no sovereignty over the Sahara, but it does possess two small enclaves on the north African coast, Ceuta and Melilla, separated from Morocco by controversial wire fences.
The enclaves have become magnets for African migrants seeking a better life in Europe.
Since January of this year, 35,000 migrants arrived in Spain, the highest number received by any EU country.