Working backward: Options for getting to Carcassonne (assuming you don't want an up-to-9-hr. train ride with a 10-minute connection at Madrid-Atocha) seem to fall out as follows:
From Seville, fly to Toulouse or Barcelona, then train. I'd fly to Toulouse if possible, because there must surely be more Toulouse-Carcassonne trains than Barcelona-Carcassonne connections.
From Cordoba, you could backtrack to Seville and take one of the flights suggested above, or you could get to Carcassonne by train in less than 8-1/2 hours.
From Gibraltar, the best option is to take a bus to Malaga and fly to Toulouse or Barcelona, then train. These are substantially more time-consuming options than departures from Cordoba and Seville, so I think you do not want Gibraltar to be your last stop before Carcassonne.
To get across the border from Lisbon, your fastest option is to fly, and I'd head to Malaga to get Gibraltar out of the way first. From Malaga you'll have a 3-1/2 to 4-hour bus ride to Gibraltar.
From Gibraltar you'll have another 4-hour bus ride to Seville.
Then Cordoba is a quick train ride (less than an hour) from Seville.
Alternatively, you can get a bus from Gibraltar to Algeciras, then a train to Cordoba (perhaps 5 hours), You would then visit Cordoba before Seville.
I obtained the trip durations from Rome2Rio.com. Don't bank on them without verifying them. If you keep drilling down you should eventually find a link to the appropriate bus company's schedule. Go to skyscanner.com to check flights. Use Renfe.com for train schedules.
Including Gibraltar in this itinerary is really costing you a lot of time. Be sure it's worth it.